package com.bonait.bnframework.common.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.model.mode.BomTypeInfo;
import com.bonait.bnframework.common.model.mode.FoodMenuInfo;
import com.bonait.bnframework.common.model.mode.GoodsParentTypeInfo;
import com.bonait.bnframework.common.model.mode.GoodsTypeInfo;
import com.bonait.bnframework.common.model.mode.GoodsUintInfo;
import com.bonait.bnframework.common.utils.ToastUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

public class GoodEditDialog extends Dialog {
    private Button yes, no;//确定按钮
    private TextView titleTv;//消息标题文本
    private Spinner editsp_cp;//菜谱
    private Spinner editsp_dl;//大类
    private Spinner editsp_xl;//小类
    private Spinner editsp_dw;//单位
    private Spinner editsp_fl;//分类
    private String titleStr;//从外界设置的title文本
    private String messageStr;//从外界设置的消息文本
    //确定文本和取消文本的显示内容
    private String yesStr, noStr;

    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param str
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
        if (str != null) {
            yesStr = str;
        }
        this.yesOnclickListener = onYesOnclickListener;
    }

    public GoodEditDialog(Context context) {
        super(context, R.style.Dialog_Msg);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_edit);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(DataVerification())
                {
                    if (yesOnclickListener != null) {
                        yesOnclickListener.onYesClick(
                                 map_cp.get(editsp_cp.getSelectedItem().toString())
                                ,map_dl.get(editsp_dl.getSelectedItem().toString())
                                ,map_xl.get(editsp_xl.getSelectedItem().toString())
                                ,map_dw.get(editsp_dw.getSelectedItem().toString())
                                ,map_fl.get(editsp_fl.getSelectedItem().toString())
                                );
                    }
                }
            }
        });

        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message
        if (titleStr != null) {
            titleTv.setText(titleStr);
        }
        if (messageStr != null) {
//            messageTv.setText(messageStr);
        }
        //如果设置按钮的文字
        if (yesStr != null) {
            yes.setText(yesStr);
        }
    }
    Map<String,String> map_cp = new LinkedHashMap<>();
    Map<String,String> map_dl = new LinkedHashMap<>();
    Map<String,String> map_xl = new LinkedHashMap<>();
    Map<String,String> map_dw = new LinkedHashMap<>();
    Map<String,String> map_fl = new LinkedHashMap<>();

    /**
     * 初始化界面控件
     */
    private void initView() {
        yes = (Button) findViewById(R.id.yes);
        no = (Button) findViewById(R.id.no);
        titleTv = (TextView) findViewById(R.id.title);
        editsp_cp = (Spinner) findViewById(R.id.editsp_cp);
        editsp_dl = (Spinner) findViewById(R.id.editsp_dl);
        editsp_xl = (Spinner) findViewById(R.id.editsp_xl);
        editsp_dw = (Spinner) findViewById(R.id.editsp_dw);
        editsp_fl = (Spinner) findViewById(R.id.editsp_fl);
        for (FoodMenuInfo item:ConfigName.getInstance().cloudData.foodMenuInfo)
        {
            map_cp.put(item.name,item.id);
        }
        for (GoodsParentTypeInfo item:ConfigName.getInstance().cloudData.goodsParentTypeInfo)
        {
            map_dl.put(item.name,item.id);
        }
        for (GoodsTypeInfo item:ConfigName.getInstance().cloudData.goodsTypeInfo)
        {
            map_xl.put(item.goodsType_Name,item.id);
        }
        for (GoodsUintInfo item:ConfigName.getInstance().cloudData.goodsUintInfo)
        {
            map_dw.put(item.name,item.id);
        }
        for (BomTypeInfo item:ConfigName.getInstance().cloudData.bomTypeInfo)
        {
            map_fl.put(item.name,item.id);
        }
        SetAdapter(editsp_cp,map_cp);
        SetAdapter(editsp_dl,map_dl);
        SetAdapter(editsp_xl,map_xl);
        SetAdapter(editsp_dw,map_dw);
        SetAdapter(editsp_fl,map_fl);
    }

    /**
     * 数据验证
     * @return
     */
    public boolean DataVerification()
    {
        boolean IsSucees=false;
        try
        {
            if(editsp_cp.getSelectedItem().toString().isEmpty())
            {
                ToastUtils.info("菜谱必须选择!");
                return false;
            }
            if(editsp_dl.getSelectedItem().toString().isEmpty())
            {
                ToastUtils.info("大类必须选择!");
                return false;
            }
            if(editsp_xl.getSelectedItem().toString().isEmpty())
            {
                ToastUtils.info("小类必须选择!");
                return false;
            }
            if(editsp_dw.getSelectedItem().toString().isEmpty())
            {
                ToastUtils.info("单位必须选择!");
                return false;
            }
            return true;
        }catch (Exception ex)
        {
            ToastUtils.error("数据验证异常!"+ex.getMessage());
            return IsSucees;
        }
    }

    public void SetAdapter(Spinner spinner,Map<String,String> map)
    {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_text_item, new ArrayList<>(map.keySet()));
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     *
     * @param message
     */
    public void setMessage(String message) {
        messageStr = message;
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        public void onYesClick(String sp,String dl,String xl,String dw,String fl);
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height= ViewGroup.LayoutParams.MATCH_PARENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }
}



    //点击弹出对话框
//    final EditDialog editDialog = new EditDialog(getActivity());
//            editDialog.setTitle("请输入电话号码");
//                    editDialog.setYesOnclickListener("确定", new EditDialog.onYesOnclickListener() {
//@Override
//public void onYesClick(String phone) {
//        if (TextUtils.isEmpty(phone)) {
//        ToastUtils.showShort(getActivity(), "请输入电话号码");
//        } else {
//        editors.putString("phone", phone);
//        editors.commit();
//        editDialog.dismiss();
//        //让软键盘隐藏
//        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.hideSoftInputFromWindow(getView().getApplicationWindowToken(), 0);
//        }
//        }
//        });
//        editDialog.setNoOnclickListener("取消", new EditDialog.onNoOnclickListener() {
//@Override
//public void onNoClick() {
//        editDialog.dismiss();
//        }
//        });
//        editDialog.show();