package com.bpa.scalage.pagefragment;
import android.os.Bundle;
import android.os.ParcelUuid;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.bpa.scalage.R;
import com.bpa.scalage.app.Main;
import com.bpa.scalage.config.ConfigName;
import com.bpa.scalage.helper.T;

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

/**
 * 其他页面
 */
public class ParameterSetFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        MainView = inflater.inflate(R.layout.parameter_set, container, false);
        Init();
        return MainView;
    }
    //region 界面变量

    //店铺信息
    private EditText Shop_Name,AutKey,Shop_DeviceId;
    //菜品放置区
    private Spinner Dishes_Com_Name,Dishes_Com_Port,environment;
    //菜品放置区
    private Spinner Plate_Com_Name,Plate_Com_Port,Plate_Com_Format;
    //当前界面
    private View MainView;
    //保存按钮
    private TextView Save;

    Map<String, Integer> material_map = new LinkedHashMap<>();
    //endregion

    //region 界面函数
    /**
     * 初始化
     */
    public void Init()
    {
        material_map.put("开发环境", 0);
        material_map.put("测试环境", 1);
        material_map.put("正式环境", 2);

        Shop_Name=MainView.findViewById(R.id.tv_dp_name);
        AutKey=MainView.findViewById(R.id.tv_dp_id);
        Shop_DeviceId=MainView.findViewById(R.id.tv_dp_devid);
        environment=MainView.findViewById(R.id.environment);
        Dishes_Com_Name=MainView.findViewById(R.id.tv_cp_com);
        Dishes_Com_Port=MainView.findViewById(R.id.tv_cp_port);
        Plate_Com_Name=MainView.findViewById(R.id.tv_cpgy_com);
        Plate_Com_Port=MainView.findViewById(R.id.tv_cpgy_port);
        Plate_Com_Format=MainView.findViewById(R.id.tv_cpgy_jy);
        Save=MainView.findViewById(R.id.parameter_save);




        SetShowText();
        Save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SaveData();
            }
        });
    }

    /**
     * 设置界面显示
     */
    public void SetShowText()
    {
        Shop_Name.setText(ConfigName.getInstance().Shop_Name);
        AutKey.setText(ConfigName.getInstance().AutKey);
        Shop_DeviceId.setText(ConfigName.getInstance().Shop_DeviceId);

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


        SelectText(environment,ConfigName.getInstance().Environment);
        SelectText(Dishes_Com_Name,ConfigName.getInstance().Dishes_Com_Name);
        SelectText(Dishes_Com_Port, Integer.toString(ConfigName.getInstance().Dishes_Com_Port));
        SelectText(Plate_Com_Name,ConfigName.getInstance().Plate_Com_Name);
        SelectText(Plate_Com_Port, Integer.toString(ConfigName.getInstance().Plate_Com_Port));
        SelectText(Plate_Com_Format,ConfigName.getInstance().Plate_Com_Format);
    }

    /**
     * 保存数据
     */
    public void SaveData()
    {
        if (Shop_Name.getText().toString().isEmpty())
        {
            T.show(MainView.getContext(),"店铺名称不能为空!");
            return;
        }
        if (AutKey.getText().toString().isEmpty())
        {
            T.show(MainView.getContext(),"店铺ID不能为空!");
            return;
        }
        if (Shop_DeviceId.getText().toString().isEmpty())
        {
            T.show(MainView.getContext(),"设备ID不能为空!");
            return;
        }

        if (Dishes_Com_Name.getSelectedItem().toString().isEmpty())
        {
            T.show(MainView.getContext(),"菜品放置区串口号不能为空!");
            return;
        }
        if (Dishes_Com_Port.getSelectedItem().toString().isEmpty())
        {
            T.show(MainView.getContext(),"菜品放置区串口端口不能为空!");
            return;
        }
        if (Plate_Com_Name.getSelectedItem().toString().isEmpty())
        {
            T.show(MainView.getContext(),"餐盘感应区域串口号不能为空!");
            return;
        }
        if (Plate_Com_Port.getSelectedItem().toString().isEmpty())
        {
            T.show(MainView.getContext(),"餐盘感应区域串口端口不能为空!");
            return;
        }
        if (Plate_Com_Format.getSelectedItem().toString().isEmpty())
        {
            T.show(MainView.getContext(),"餐盘感应区域数据位、校验位、停止位不能为空!");
            return;
        }
        ConfigName.getInstance().Environment = environment.getSelectedItem().toString();
        ConfigName.getInstance().Shop_Name=Shop_Name.getText().toString();
        ConfigName.getInstance().AutKey=AutKey.getText().toString();
        ConfigName.getInstance().Shop_DeviceId=Shop_DeviceId.getText().toString();
        ConfigName.getInstance().Dishes_Com_Name=Dishes_Com_Name.getSelectedItem().toString();
        ConfigName.getInstance().Dishes_Com_Port=Integer.valueOf(Dishes_Com_Port.getSelectedItem().toString()).intValue();
        ConfigName.getInstance().Plate_Com_Name=Plate_Com_Name.getSelectedItem().toString();
        ConfigName.getInstance().Plate_Com_Port=Integer.valueOf(Plate_Com_Port.getSelectedItem().toString()).intValue();
        ConfigName.getInstance().Plate_Com_Format=Plate_Com_Format.getSelectedItem().toString();
        Main.getInstance().SavePZ();
        T.show(MainView.getContext(),"配置保存成功!");
    }

    /**
     * 选中下拉框
     * @param spinner
     * @param value
     */
    public void SelectText(Spinner spinner,String value)
    {
        SpinnerAdapter apsAdapter= spinner.getAdapter(); //得到SpinnerAdapter对象
        int k= apsAdapter.getCount();
        for(int i=0;i<k;i++){

            if(value.equals(apsAdapter.getItem(i).toString())){
                spinner.setSelection(i,true);// 默认选中项
                break;
            }
        }
    }

    //endregion
}