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

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

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

import com.example.bpa.R;
import com.example.bpa.config.ConfigName;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.mode.BPA_GOODS;
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.List;

/**
 * Mqtt配置
 */
public class mqttparameter extends Fragment implements View.OnClickListener{

    //region 界面变量
    /**
     * 界面实例
     */
    private View view;
    /**
     * 输入变量
     */
    EditText passwayText_1,passwayText_2,passwayText_3,passwayText_4,passwayText_5;
    /**
     * 保存参数设置
     */
    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_mqttparameter, 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);
        editTextLists = Arrays.asList(passwayText_1,passwayText_2,passwayText_3,passwayText_4,passwayText_5);
        //保存按钮
        SaveButton = view.findViewById(R.id.StartButton);
    }
    /**
     * 初始化按钮事件
     */
    private void initEvents()
    {
        SaveButton.setOnClickListener(this);
    }

    /**
     * 初始化数据加载
     */
    public void Initdata() {
        bpa_systemsets= QueryDB.GetSystemsetALL();
        for (BPA_SYSTEMSET item:bpa_systemsets)
        {
            switch (item.type)
            {
                case 12:
                    ConfigName.getInstance().mqtt_userName=item.value;
                    passwayText_1.setText(item.value);
                    break;
                case 13:
                    ConfigName.getInstance().mqtt_passWord=item.value;
                    passwayText_2.setText(item.value);
                    break;
                case 14:
                    ConfigName.getInstance().mqtt_ip=item.value;
                    passwayText_3.setText(item.value);
                    break;
                case 15:
                    ConfigName.getInstance().mqtt_post=Integer.parseInt(item.value);
                    passwayText_4.setText(item.value);
                    break;
                case 16:
                    ConfigName.getInstance().mqtt_topic=item.value;
                    passwayText_5.setText(item.value);
                    break;
            }
        }
    }
    //endregion

    //region 界面点击事件
    /**
     * 本页面点击事件监听22
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.StartButton://保存参数
                ConfigName.getInstance().mqtt_userName=passwayText_5.getText().toString();
                ConfigName.getInstance().mqtt_passWord=passwayText_5.getText().toString();
                ConfigName.getInstance().mqtt_ip=passwayText_5.getText().toString();
                ConfigName.getInstance().mqtt_post=Integer.parseInt(passwayText_4.getText().toString());
                ConfigName.getInstance().mqtt_topic=passwayText_5.getText().toString();

                for (BPA_SYSTEMSET item:bpa_systemsets)
                {
                    if(item.type>=12 && item.type<=16)
                    {
                        QueryDB.DeleteSystemset(item.id);
                    }
                }
                for (int i = 0; i <editTextLists.size();i++){
                    BPA_SYSTEMSET set=new BPA_SYSTEMSET();
                    set.type=i+12;
                    set.value=editTextLists.get(i).getText().toString();
                    set.deviceID = ConfigName.getInstance().DeviceId;
                    set.userID = ConfigName.getInstance().user.userID;
                    QueryDB.AddSystemset(set);
                }
                T.show(view.getContext(), "保存成功!");

                break;
        }
    }
    //endregion
}