package com.bjzc.zjyxdxj.weight;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.bjzc.zjyxdxj.R;
import com.bjzc.zjyxdxj.adapter.CommonAdapter;
import java.util.List;


/**
 * Created by Rehome-rjb1 on 2017/5/18.
 * 列表模式的dialog
 */

public class ListDialog extends Dialog {

    ListView lv;

    TextView tvTitle;

    LinearLayout llTitle;

    TextView dialogCancel;

    TextView dialogCommit;

    LinearLayout layDialogCancel;

    private CommonAdapter<String> adapter;
    private List<String> datas;

    private ListDialogListener listener;


    public ListDialog(@NonNull Context context, List<String> datas, ListDialogListener listener) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setContentView(R.layout.layout_take_out_time);
        findView();
        this.listener = listener;
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (getScreenWidth(context)) * 2 / 3;
        window.setGravity(Gravity.CENTER);
        setCancelable(false);

        this.datas = datas;
        if (datas != null) {
            setAdapter();
        }
    }

    private void findView(){
        tvTitle = findViewById(R.id.tv_title);
        lv = findViewById(R.id.lv);
        llTitle = findViewById(R.id.ll_title);
        dialogCancel = findViewById(R.id.dialog_cancel);
        dialogCommit = findViewById(R.id.dialog_commit);
        layDialogCancel = findViewById(R.id.lay_dialog_cancel);
        dialogCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        dialogCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    int position = lv.getCheckedItemPosition();
                    Log.i("app",String.valueOf(position));
                    listener.selectText(datas.get(position), position);
                }
            }
        });
    }

    public void setTvTitle(String title) {
        tvTitle.setText(title);
    }

    public void hideTitle() {
        llTitle.setVisibility(View.GONE);
    }

    private void setAdapter() {

        if (adapter == null) {
//            adapter = new CommonAdapter<String>(getContext(), R.layout.item_content, datas) {
//                @Override
//                protected void convert(ViewHolder viewHolder, String item, int position) {
//                    viewHolder.setText(R.id.tv_text, item);
//                }
//            };

            ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(),android.R.layout.simple_list_item_single_choice,datas);

            lv.setAdapter(adapter);
            // 必选设置为单选
            lv.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
            if(datas!=null&&datas.size()>0){
                lv.setItemChecked(0,true);
            }

//            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                @Override
//                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                    if (listener != null) {
//                        listener.selectText(datas.get(position), position);
//                        //dismiss();
//                    }
//                }
//            });

        } else {
            adapter.notifyDataSetChanged();
        }

    }

    //获取屏幕宽度
    public static int getScreenWidth(Context context) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }

    public interface ListDialogListener {
        void selectText(String text, int position);
    }
}
