package com.example.bpa.view.from;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.example.bpa.Model.IWriteCallBack;
import com.example.bpa.Model.Res_PLCADDRESS;
import com.example.bpa.R;
import com.example.bpa.config.ConfigName;
import com.example.bpa.helper.T;
import com.example.bpa.service.DeviceData;
import com.example.bpa.view.control.sdkz_control;
import com.example.bpa.view.control.tab_control;

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

public class sdkz_new_Activity extends AppCompatActivity implements View.OnClickListener{

    //region 变量
    /**
     * 返回按钮
     */
    ImageView gongneng_fanhui;
    /**
     * 标题设置
     */
    TextView gongneng_title;

    /**
     *
     */
    LinearLayout sdkz_linrarlaout;
    Context context;
    Activity activity;
    //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_new);
        context=this.getBaseContext();
        activity=this;
        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);
        sdkz_linrarlaout= this.findViewById(R.id.sdkz_linrarlaout);
        //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据
        String msg = getIntent().getStringExtra("data");
        gongneng_title.setText(msg);

        //校准开关
        Initdata();
    }

    /**
     * 初始化按钮事件
     */
    private void initEvents() {
        gongneng_fanhui.setOnClickListener(this);
        //switch

    }

    /**
     * 初始化数据加载
     */
    public void Initdata() {
        try {
            DeviceData.Get().setHandOrAutoSwitch(false, null);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {

                                    List<Res_PLCADDRESS> plcaddresses=new ArrayList<>();
                                    for (Res_PLCADDRESS item : ConfigName.getInstance().PlcAddress_配料吧_手动) {
                                        if(item.address.equals("-------------"))//这是标题
                                        {
                                            if(plcaddresses.size()>0)
                                            {
                                                sdkz_control sdkz = new sdkz_control(context, null, plcaddresses,activity,true);
                                                sdkz_linrarlaout.addView(sdkz);
                                            }
                                            plcaddresses=new ArrayList<>();
                                            tab_control tab = new tab_control(context, null, item.name);
                                            sdkz_linrarlaout.addView(tab);
                                        }else
                                        {
                                            plcaddresses.add(item);
                                        }
                                    }
                                    if(plcaddresses.size()>0)
                                    {
                                        sdkz_control sdkz = new sdkz_control(context, null, plcaddresses,activity,true);
                                        sdkz_linrarlaout.addView(sdkz);
                                    }
                                } catch (Exception e) {

                                }
                            }
                        });
                    } catch (Exception e) {

                    }
                }
            }).start();
        } catch (Exception e) {

        }
    }
    //endregion

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