package com.example.bpa.view.from;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.bpa.Model.IWriteCallBack;
import com.example.bpa.R;
import com.example.bpa.db.QueryDB;
import com.example.bpa.helper.DateUtils;
import com.example.bpa.helper.T;
import com.example.bpa.service.DeviceData;
import com.example.bpa.view.adapter.ddgl_adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class sdkz_activity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    //region 变量
    /**
     * 返回按钮
     */
    ImageView gongneng_fanhui;
    /**
     * 标题设置
     */
    TextView gongneng_title;
    /**
     * 通道校准开关
     */
    Switch Switch_1, Switch_2, Switch_3, Switch_4, Switch_5, Switch_6, Switch_7, Switch_8, Switch_9, Switch_10,
            Switch_11, Switch_12, Switch_13, Switch_14, Switch_15, Switch_16, Switch_17, Switch_18;
    List<Switch> switchLists = new ArrayList<Switch>();
    //endregion

    //region 私有函数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdkz);
        Init();
        initEvents();
    }

    @Override
    protected void onDestroy() {

        try {
            //设置自动模式
            DeviceData.Get().setHandOrAutoSwitch(true, null);
        } catch (Exception e) {

        }
        super.onDestroy();
    }
    //endregion

    //region 公共函数

    /**
     * 初始化
     */
    private void Init(){
        gongneng_fanhui = this.findViewById(R.id.gongneng_fanhui);
        gongneng_title = this.findViewById(R.id.gongneng_title);

        //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据
        String msg = getIntent().getStringExtra("data");
        gongneng_title.setText(msg);

        //校准开关
        Switch_1 = findViewById(R.id.Switch_1);
        Switch_2 = findViewById(R.id.Switch_2);
        Switch_3 = findViewById(R.id.Switch_3);
        Switch_4 = findViewById(R.id.Switch_4);
        Switch_5 = findViewById(R.id.Switch_5);
        Switch_6 = findViewById(R.id.Switch_6);
        Switch_7 = findViewById(R.id.Switch_7);
        Switch_8 = findViewById(R.id.Switch_8);
        Switch_9 = findViewById(R.id.Switch_9);
        Switch_10 = findViewById(R.id.Switch_10);
        Switch_11 = findViewById(R.id.Switch_11);
        Switch_12 = findViewById(R.id.Switch_12);
        Switch_13 = findViewById(R.id.Switch_13);
        Switch_14 = findViewById(R.id.Switch_14);
        Switch_15 = findViewById(R.id.Switch_15);
        Switch_16 = findViewById(R.id.Switch_16);
        Switch_17 = findViewById(R.id.Switch_17);
        Switch_18 = findViewById(R.id.Switch_18);
        switchLists = Arrays.asList(Switch_1, Switch_2, Switch_3, Switch_4, Switch_5, Switch_6, Switch_7, Switch_8, Switch_9, Switch_10,
                Switch_11, Switch_12,Switch_13, Switch_14, Switch_15, Switch_16, Switch_17, Switch_18);
        Initdata();
    }

    /**
     * 初始化按钮事件
     */
    private void initEvents() {
        gongneng_fanhui.setOnClickListener(this);
        //switch
        Switch_1.setOnCheckedChangeListener(this);
        Switch_2.setOnCheckedChangeListener(this);
        Switch_3.setOnCheckedChangeListener(this);
        Switch_4.setOnCheckedChangeListener(this);
        Switch_5.setOnCheckedChangeListener(this);
        Switch_6.setOnCheckedChangeListener(this);
        Switch_7.setOnCheckedChangeListener(this);
        Switch_8.setOnCheckedChangeListener(this);
        Switch_9.setOnCheckedChangeListener(this);
        Switch_10.setOnCheckedChangeListener(this);
        Switch_11.setOnCheckedChangeListener(this);
        Switch_12.setOnCheckedChangeListener(this);
        Switch_13.setOnCheckedChangeListener(this);
        Switch_14.setOnCheckedChangeListener(this);
        Switch_15.setOnCheckedChangeListener(this);
        Switch_16.setOnCheckedChangeListener(this);
        Switch_17.setOnCheckedChangeListener(this);
        Switch_18.setOnCheckedChangeListener(this);
    }

    /**
     * 初始化数据加载
     */
    public void Initdata() {
        try {

            //设置手动模式
            DeviceData.Get().setHandOrAutoSwitch(false, null);
        } catch (Exception e) {

        }
    }
    //endregion

    //region 点击事件
    /**
     * 本页面点击事件监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gongneng_fanhui://返回按钮
                this.finish();
                break;
            case R.id.button://查询按钮
                Initdata();
                break;
        }
    }

    /**
     * 开关按钮
     * @param compoundButton
     * @param b
     */
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if(compoundButton.getTag()==null)
            return;
        String plcadder= compoundButton.getTag().toString();
        DeviceData.Get().setSwitch(b, plcadder, new IWriteCallBack() {
            @Override
            public void onSuccess() {
                T.show(sdkz_activity.this,compoundButton.getText().toString()+(b?"打开":"关闭") + "成功");
            }

            @Override
            public void onFailure(String ErrorMsg) {
                T.show(sdkz_activity.this,compoundButton.getText().toString()+(b?"打开":"关闭")+ "失败");
            }
        });
    }
    //endregion
}