package com.hw.nativeapp.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.hw.nativeapp.R;
import com.hw.nativeapp.httpnet.entity.RelatiInfoBean;

import java.util.List;


public class DialogDropDown extends Dialog {

    public DialogDropDown(Context mcontext) {
        super(mcontext);
    }

    public static class Builder {
        private Context context;
        private String negative_text;//消极的
        private String positive_text;//积极的
        private boolean negativeShow = false;
        private boolean positiveShow = false;
        private OnClickListener negativeListener;//消极的监听
        private OnClickListener positiveListener;//积极的监听

        private List<RelatiInfoBean> dataList;
        private String relatiId;//物料理化关系id
        private Integer relatiValue;//物料理化关系值

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setData(List<RelatiInfoBean> data) {
            dataList = data;
            return this;
        }

        public String getRelatiId(){
            if (relatiId != null){
                return relatiId;
            }
            return "";
        }
        public Integer getRelatiValue(){
            if (relatiValue != null){
                return relatiValue;
            }
            return 0;
        }

        public DialogDropDown.Builder setNegativeButton(String negative_text, OnClickListener negativeListener) {
            if (negative_text == null) {
                this.negative_text = "取消";
            }
            this.negative_text = negative_text;
            this.negativeListener = negativeListener;
            this.negativeShow = true;
            return this;
        }

        public DialogDropDown.Builder setPositionButton(String positive_text, OnClickListener positiveListener) {
            if (positive_text == null) {
                this.positive_text = "确定";
            }
            this.positive_text = positive_text;
            this.positiveListener = positiveListener;
            this.positiveShow = true;
            return this;
        }

        private Spinner m_spiModel;
        private TextView btnupEnter;
        private TextView btnupCancel;


        public DialogDropDown create() {
            final DialogDropDown dialog = new DialogDropDown(context);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_drop_down, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            m_spiModel = (Spinner) view.findViewById(R.id.patrol_relati);

            btnupEnter = (TextView) view.findViewById(R.id.btn_quantity_enter);
            btnupCancel = (TextView) view.findViewById(R.id.btn_quantity_cancel);


            String strModel_init="";
            for(int i=0;i<dataList.size();i++) {
                if(i < dataList.size()) {
                    strModel_init += dataList.get(i).relatiName + ",";
                } else {
                    strModel_init += dataList.get(i).relatiName;
                }
            }
            String[] arrModel =strModel_init.split(",");//转换 数组
            ArrayAdapter<String> adapter;//数组 配置器 下拉菜单赋值用
            //将可选内容与ArrayAdapter连接起来
            adapter = new ArrayAdapter<String>(this.context,android.R.layout.simple_spinner_item,arrModel);
            //设置下拉列表的风格
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            m_spiModel.setAdapter(adapter);//将adapter 添加到spinner中
            m_spiModel.setVisibility(View.VISIBLE);//设置默认值
            m_spiModel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {//选择item的选择点击监听事件
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    for(int i=0;i<dataList.size();i++) {
                        if(i == position){
                            relatiId = dataList.get(i).relatiId;
                            relatiValue = dataList.get(i).relatiValue;
                        }
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

            btnupCancel.setText(negative_text);
            btnupEnter.setText(positive_text);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            btnupCancel.setOnClickListener(v -> negativeListener.onClick(dialog, Dialog.BUTTON_NEGATIVE));
            btnupEnter.setOnClickListener(v -> positiveListener.onClick(dialog, Dialog.BUTTON_POSITIVE));
            if (negativeShow){
                btnupCancel.setVisibility(View.VISIBLE);
            }
            if (positiveShow){
                btnupEnter.setVisibility(View.VISIBLE);
            }
            return dialog;
        }
    }
}
