package com.bonait.bnframework.modules.home.fragment.from.fragment;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ConfigData;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_SYSTEMSET;
import com.bonait.bnframework.common.model.mode.VersionSelectionEnum;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.orhanobut.logger.Logger;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SystemSystemsetFragment extends BaseFragment {

    @BindView(R.id.edittext1)
    EditText edittext1;
    @BindView(R.id.edittext2)
    EditText edittext2;
    @BindView(R.id.edittext3)
    EditText edittext3;
    @BindView(R.id.edittext4)
    EditText edittext4;

    @BindView(R.id.edittext100)
    EditText edittext100;
    @BindView(R.id.edittext101)
    EditText edittext101;
    @BindView(R.id.edittext102)
    EditText edittext102;
    @BindView(R.id.environment)
    Spinner environment;

    @BindView(R.id.versionselection)
    Spinner versionselection;

    @BindView(R.id.HuoLi)
    Spinner HuoLi;

    @BindView(R.id.isStop)
    Spinner isStop;


    @BindView(R.id.edittext_wd)
    EditText edittext_wd;

    ArrayList<EditText> editTextLists = new ArrayList<>();

    Map<String, Integer> material_map_yes_no = new LinkedHashMap<>();
    Map<String, Integer> material_map = new LinkedHashMap<>();
    Map<String, Integer> material_map_vis = new LinkedHashMap<>();

    /**
     * 系统设置参数
     */
    ArrayList<BPA_SYSTEMSET> bpa_systemsets = new ArrayList<>();
    private Context context;

    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_system_systemset, null);
        ButterKnife.bind(this, root);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Logger.d("我的fragment创建");
        context = getContext();
        initData();
        initSelect();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.d("我的fragment销毁");
    }

    /**
     * 当在activity设置viewPager + BottomNavigation + fragment时，
     * 为防止viewPager左滑动切换界面，与fragment左滑返回上一界面冲突引起闪退问题，
     * 必须加上此方法，禁止fragment左滑返回上一界面。
     * <p>
     * 切记！切记！切记！否则会闪退！
     * <p>
     * 当在fragment设置viewPager + BottomNavigation + fragment时，则不会出现这个问题。
     */
    @Override
    protected boolean canDragBack() {
        return false;
    }

    private void initData() {
        editTextLists.add(edittext1);
        editTextLists.add(edittext2);
        editTextLists.add(edittext3);
        editTextLists.add(edittext4);

        material_map.put("开发环境", 0);
        material_map.put("测试环境", 1);
        material_map.put("正式环境", 2);

        material_map_yes_no.put("是", 0);
        material_map_yes_no.put("否", 1);


        for (int i = 0; i < ConfigName.getInstance().versionSelectionValues.length; i++) {
            material_map_vis.put(ConfigName.getInstance().versionSelectionValues[i], i);
        }


        ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.spinner_text_item, new ArrayList<>(material_map.keySet()));
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        environment.setAdapter(adapter);

        ArrayAdapter<String> adapter1 = new ArrayAdapter<>(context, R.layout.spinner_text_item, new ArrayList<>(material_map_vis.keySet()));
        adapter1.setDropDownViewResource(R.layout.spinner_dropdown_item);
        versionselection.setAdapter(adapter1);

        ArrayAdapter<String> adapter11 = new ArrayAdapter<>(context, R.layout.spinner_text_item, new ArrayList<>(material_map_yes_no.keySet()));
        adapter11.setDropDownViewResource(R.layout.spinner_dropdown_item);
        isStop.setAdapter(adapter11);
        isStop.setSelection(material_map_yes_no.get(ConfigName.getInstance().WhetherManualFeedingSuspended));


        ArrayAdapter<String> adapter2 = new ArrayAdapter<>(context, R.layout.spinner_text_item, new ArrayList<>(ConfigName.getInstance().HeatingGearL.keySet()));
        adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);
        HuoLi.setAdapter(adapter2);
        HuoLi.setSelection(ConfigName.getInstance().HeatingGearL.get(ConfigName.getInstance().HuoLi));

        bpa_systemsets = QueryDB.GetSystemsetALL();
        for (BPA_SYSTEMSET item : bpa_systemsets) {
            switch (item.type) {
                case -200:
                    ConfigName.getInstance().MainAddress = item.value;
                    edittext100.setText(item.value);
                    break;
                case -201:
                    ConfigName.getInstance().MainPost = Integer.parseInt(item.value);
                    edittext101.setText(item.value);
                    break;
                case -202:
                    ConfigName.getInstance().DeviceHao = Integer.parseInt(item.value);
                    edittext102.setText(item.value);
                    break;
                case -100:
                    ConfigName.getInstance().versionSelectionEnum = item.value;
                    versionselection.setSelection(material_map_vis.get(item.value)==null?0:material_map_vis.get(item.value));
                    break;
                case -99:
                    ConfigName.getInstance().Environment = item.value;
                    environment.setSelection(material_map.get(item.value));
                    break;
                case 1:
                    ConfigName.getInstance().Address = item.value;
                    edittext1.setText(item.value);
                    break;
                case 2:
                    ConfigName.getInstance().Post = Integer.parseInt(item.value);
                    edittext2.setText(item.value);
                    break;
                case 3:
                    ConfigName.getInstance().ClientAutoKey = item.value;
                    edittext3.setText(item.value);
                    break;
                case 4:
                    ConfigName.getInstance().DeviceAutoKey = item.value;
                    edittext4.setText(item.value);
                    break;

            }
        }

        edittext_wd.setText(ConfigName.getInstance().MaxTemp+"");
    }

    /**
     * 初始化选中
     */
    public void initSelect() {
        HuoLi.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            /*public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }*/
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                ConfigName.getInstance().HuoLi=HuoLi.getSelectedItem().toString();
                ConfigData.getInstance().SavePZ();
            }

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

            }
        });

        isStop.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            /*public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }*/
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                ConfigName.getInstance().WhetherManualFeedingSuspended=isStop.getSelectedItem().toString();
                ConfigData.getInstance().SavePZ();
            }

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

            }
        });

    }

    @OnClick({R.id.StartButton})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.StartButton://保存按钮
                ConfigName.getInstance().Address = edittext1.getText().toString();
                ConfigName.getInstance().Post = Integer.parseInt(edittext2.getText().toString());
                ConfigName.getInstance().MainAddress = edittext100.getText().toString();
                ConfigName.getInstance().MainPost = Integer.parseInt(edittext101.getText().toString());
                ConfigName.getInstance().DeviceHao = Integer.parseInt(edittext102.getText().toString());

                ConfigName.getInstance().ClientAutoKey = edittext3.getText().toString();
                ConfigName.getInstance().DeviceAutoKey = edittext4.getText().toString();

                ConfigName.getInstance().Environment = environment.getSelectedItem().toString();

                boolean isgb = false;
                if (!ConfigName.getInstance().versionSelectionEnum.equals(versionselection.getSelectedItem().toString())) {
                    ConfigName.getInstance().versionSelectionEnum = versionselection.getSelectedItem().toString();
                    isgb = true;
                }

                for (BPA_SYSTEMSET item : bpa_systemsets) {
                    QueryDB.DeleteSystemset(item.id);
                }

                for (int i = 0; i < editTextLists.size(); i++) {
                    BPA_SYSTEMSET set = new BPA_SYSTEMSET();
                    set.type = i + 1;
                    set.value = editTextLists.get(i).getText().toString();
                    set.deviceID = ConfigName.getInstance().DeviceId;
                    set.userID = ConfigName.getInstance().user.userID;
                    QueryDB.AddSystemset(set);
                }
                BPA_SYSTEMSET set = new BPA_SYSTEMSET();
                set.type = -99;
                set.value = environment.getSelectedItem().toString();
                set.deviceID = ConfigName.getInstance().DeviceId;
                set.userID = ConfigName.getInstance().user.userID;
                QueryDB.AddSystemset(set);

                BPA_SYSTEMSET set_vis = new BPA_SYSTEMSET();
                set_vis.type = -100;
                set_vis.value = versionselection.getSelectedItem().toString();
                set_vis.deviceID = ConfigName.getInstance().DeviceId;
                set_vis.userID = ConfigName.getInstance().user.userID;
                QueryDB.AddSystemset(set_vis);

                BPA_SYSTEMSET set_main1 = new BPA_SYSTEMSET();
                set_main1.type = -200;
                set_main1.value = edittext100.getText().toString();
                set_main1.deviceID = ConfigName.getInstance().DeviceId;
                set_main1.userID = ConfigName.getInstance().user.userID;
                QueryDB.AddSystemset(set_main1);

                BPA_SYSTEMSET set_main2 = new BPA_SYSTEMSET();
                set_main2.type = -201;
                set_main2.value =edittext101.getText().toString();
                set_main2.deviceID = ConfigName.getInstance().DeviceId;
                set_main2.userID = ConfigName.getInstance().user.userID;
                QueryDB.AddSystemset(set_main2);

                BPA_SYSTEMSET set_main3 = new BPA_SYSTEMSET();
                set_main3.type = -202;
                set_main3.value =edittext102.getText().toString();
                set_main3.deviceID = ConfigName.getInstance().DeviceId;
                set_main3.userID = ConfigName.getInstance().user.userID;
                QueryDB.AddSystemset(set_main3);

                if (isgb) {
                    ConfigData.getInstance().RevertPLCProcess();
                }


                ConfigName.getInstance().HuoLi=HuoLi.getSelectedItem().toString();
                ConfigName.getInstance().WhetherManualFeedingSuspended=isStop.getSelectedItem().toString();

                if(!edittext_wd.getText().toString().isEmpty())
                {
                    ConfigName.getInstance().MaxTemp=Integer.parseInt(edittext_wd.getText().toString());
                }
                ConfigData.getInstance().SavePZ();
                ToastUtils.info("保存成功");
                break;

        }
    }
}