package com.example.bpa.view.fragment.setting;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

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

import com.example.bpa.R;
import com.example.bpa.config.ConfigName;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.mode.BPA_SILOS;
import com.example.bpa.db.mode.BPA_SYSTEMSET;
import com.example.bpa.helper.T;

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

/**
 * 系统设置
 */
public class systemparameter extends Fragment implements View.OnClickListener{

    //region 界面变量
    /**
     * 界面实例
     */
    private View view;
    /**
     * 输入变量
     */
    EditText passwayText_1,passwayText_2,passwayText_3,passwayText_4,passwayText_5,passwayText_6,passwayText_7,passwayText_8,passwayText_9,passwayText_10,passwayText_11;
    Spinner environment;
    Map<String,Integer> material_map = new LinkedHashMap<>();

    /**
     * 保存参数设置
     */
    Button SaveButton;
    /**
     * 系统设置参数
     */
    ArrayList<BPA_SYSTEMSET>  bpa_systemsets=new ArrayList<>();

    List<EditText> editTextLists  = new ArrayList<EditText>();
    //endregion

    //region 私有函数
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_systemparameter, container,false);
        Init();
        initEvents();
        Initdata();
        return view;
    }
    //endregion

    //region 公有函数

    /**
     * 初始化
     */
    public void Init()
    {
        passwayText_1 = view.findViewById(R.id.passwayText_1);
        passwayText_2 = view.findViewById(R.id.passwayText_2);
        passwayText_3 = view.findViewById(R.id.passwayText_3);
        passwayText_4 = view.findViewById(R.id.passwayText_4);
        passwayText_5 = view.findViewById(R.id.passwayText_5);
        passwayText_6 = view.findViewById(R.id.passwayText_6);
        passwayText_7 = view.findViewById(R.id.passwayText_7);
        passwayText_8 = view.findViewById(R.id.passwayText_8);
        passwayText_9 = view.findViewById(R.id.passwayText_9);
        passwayText_10 = view.findViewById(R.id.passwayText_10);
        passwayText_11 = view.findViewById(R.id.passwayText_11);
        environment=view.findViewById(R.id.environment);
        editTextLists = Arrays.asList(passwayText_1,passwayText_2,passwayText_3,passwayText_4,passwayText_5,passwayText_6,passwayText_7,passwayText_8,passwayText_9,passwayText_10,passwayText_11);
        //保存按钮
        SaveButton = view.findViewById(R.id.StartButton);
        material_map.put("开发环境",0);
        material_map.put("测试环境",1);
        material_map.put("正式环境",2);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(view.getContext(), R.layout.spinner_text_item, new ArrayList<>(material_map.keySet()));
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
        environment.setAdapter(adapter);
    }
    /**
     * 初始化按钮事件
     */
    private void initEvents()
    {
        SaveButton.setOnClickListener(this);
    }

    /**
     * 初始化数据加载
     */
    public void Initdata() {
        bpa_systemsets=QueryDB.GetSystemsetALL();
        for (BPA_SYSTEMSET item:bpa_systemsets)
        {
            switch (item.type)
            {
                case 1:
                    ConfigName.getInstance().OrderPushTime=Integer.parseInt(item.value);
                    passwayText_1.setText(item.value);
                    break;
                case 2:
                    ConfigName.getInstance().SilosValuePushTime=Integer.parseInt(item.value);
                    passwayText_2.setText(item.value);
                    break;
                case 3:
                    ConfigName.getInstance().OtherValuePushTime=Integer.parseInt(item.value);
                    passwayText_3.setText(item.value);

                    break;
                case 4:
                    ConfigName.getInstance().PipeDrainCycleTime=Integer.parseInt(item.value);
                    passwayText_4.setText(item.value);
                    break;
                case 5:
                    ConfigName.getInstance().OrderTimeout=Integer.parseInt(item.value);
                    passwayText_5.setText(item.value);
                    break;
                case 6:
                    ConfigName.getInstance().SiloFeedThreshold=Integer.parseInt(item.value);
                    passwayText_6.setText(item.value);
                    break;
                case 7:
                    ConfigName.getInstance().SiloAlarmThreshold=Integer.parseInt(item.value);
                    passwayText_7.setText(item.value);
                    break;
                case 8:
                    ConfigName.getInstance().AICleanTime=Integer.parseInt(item.value);
                    passwayText_8.setText(item.value);
                    break;
                case 9:
                    ConfigName.getInstance().AutKey=item.value;
                    passwayText_9.setText(item.value);
                    break;
                case 10:
                    ConfigName.getInstance().Address=item.value;
                    passwayText_10.setText(item.value);
                    break;
                case 11:
                    ConfigName.getInstance().Post=Integer.parseInt(item.value);
                    passwayText_11.setText(item.value);
                    break;
                case 12:
                    ConfigName.getInstance().Environment=item.value;
                    environment.setSelection(material_map.get(item.value));
                    break;

            }

        }
    }
    //endregion

    //region 界面点击事件
    /**
     * 本页面点击事件监听22
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.StartButton://保存参数
                ConfigName.getInstance().OrderPushTime=Integer.parseInt(passwayText_1.getText().toString());
                ConfigName.getInstance().SilosValuePushTime=Integer.parseInt(passwayText_2.getText().toString());
                ConfigName.getInstance().OtherValuePushTime=Integer.parseInt(passwayText_3.getText().toString());
                ConfigName.getInstance().PipeDrainCycleTime=Integer.parseInt(passwayText_4.getText().toString());
                ConfigName.getInstance().OrderTimeout=Integer.parseInt(passwayText_5.getText().toString());
                ConfigName.getInstance().SiloFeedThreshold=Integer.parseInt(passwayText_6.getText().toString());
                ConfigName.getInstance().SiloAlarmThreshold=Integer.parseInt(passwayText_7.getText().toString());
                ConfigName.getInstance().AICleanTime=Integer.parseInt(passwayText_8.getText().toString());
                ConfigName.getInstance().AutKey=passwayText_9.getText().toString();
                ConfigName.getInstance().Address=passwayText_10.getText().toString();
                ConfigName.getInstance().Post=Integer.parseInt(passwayText_11.getText().toString());
                ConfigName.getInstance().Environment=environment.getSelectedItem().toString();
                for (BPA_SYSTEMSET item:bpa_systemsets)
                {
                    if(item.type<13)
                    {
                        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=12;
                set.value=environment.getSelectedItem().toString();
                set.deviceID = ConfigName.getInstance().DeviceId;
                set.userID = ConfigName.getInstance().user.userID;
                QueryDB.AddSystemset(set);

                ArrayList<BPA_SILOS> silos=QueryDB.GetSilosALL();
                for (BPA_SILOS item:silos)
                {
                    //if(item.thrsoleValue<=0)
                    {
                        item.thrsoleValue=ConfigName.getInstance().SiloAlarmThreshold;
                    }
                    //if(item.warningValue<=0)
                    {
                        item.warningValue=ConfigName.getInstance().SiloFeedThreshold;
                    }
                    QueryDB.UpdateSilos(item);
                }
                T.show(view.getContext(), "保存成功!");

                break;
        }
    }
    //endregion
}