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

import static com.bonait.bnframework.MainApplication.getContext;

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

import android.content.Context;
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.BaseActivity;
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_SYSTEMSET;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.fragment.GuanLifragment;
import com.bonait.bnframework.modules.home.fragment.SheZhifragment;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.QMUIViewPager;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;

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

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

public class SystemParameterActivity extends BaseActivity {

    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;
    private Context context;

    @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.environment)
    Spinner environment;

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



    ArrayList<EditText> editTextLists = new ArrayList<>();
    Map<String, Integer> material_map = new LinkedHashMap<>();
    Map<String, Integer> material_map_vis = new LinkedHashMap<>();

    /**
     * 系统设置参数
     */
    ArrayList<BPA_SYSTEMSET> bpa_systemsets = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_systemparameter);
        ButterKnife.bind(this);
        context=getContext();
        initTopBar();
        initFragment();
        initData();
        initSelect();
    }

    private void initTopBar() {
        mTopBar.setTitle("系统参数");
        mTopBar.addLeftImageButton(R.mipmap.fanhui,R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();              }
        });
    }
    /**
     * viewPager里添加fragment
     */
    private void initFragment() {

    }

    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);

        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);


        bpa_systemsets = QueryDB.GetSystemsetALL();
        for (BPA_SYSTEMSET item : bpa_systemsets) {
            switch (item.type) {
                case -100:
                    ConfigName.getInstance().versionSelectionEnum = item.value;
                    versionselection.setSelection(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;

            }
        }
    }

    /**
     * 初始化选中
     */
    public void initSelect() {


    }

    @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().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);
                if (isgb) {
                    ConfigData.getInstance().RevertPLCProcess();
                }
                ToastUtils.info("保存成功");
                break;

        }
    }
    //-------------------------配置viewPager与fragment关联----------------------------//

    @Override
    public void onDestroy() {
        super.onDestroy();
        environment.setAdapter(null);
        versionselection.setAdapter(null);
        editTextLists.clear();
        editTextLists = null;
        material_map.clear();
        material_map = null;
        material_map_vis.clear();
        material_map_vis = null;
        bpa_systemsets.clear();
        bpa_systemsets = null;
        context= null;
    }

    @Override
    protected boolean canDragBack() {
        return false;
    }
}