package com.bonait.bnframework.modules.home.fragment.mode;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Switch;

import androidx.annotation.Nullable;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.res.Res_PLCADDRESS1;
import com.bonait.bnframework.common.helper.AutoNextLineLinearLayout;
import com.bonait.bnframework.common.utils.ToastUtils;

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

public class sdkz_control extends LinearLayout {
    AutoNextLineLinearLayout tagLayout;
    private View root;

    public sdkz_control(Context context, @Nullable AttributeSet attrs, List<Res_PLCADDRESS1> address, Activity activity, boolean Issd) {
        super(context, attrs);
        root= LayoutInflater.from(context).inflate(R.layout.wrapanl_sdkz_item, this);
        tagLayout=this.findViewById(R.id.tagLayout);
        Init(address,activity,Issd);
    }

    /**
     *       <Switch
     *                         android:id="@+id/out_switch"
     *                         android:layout_width="wrap_content"
     *                         android:layout_height="wrap_content"
     *                         android:background="@drawable/qs_switch"
     *                         android:text="出口称重模块校准"
     *                         android:layout_margin="5dp"/>
     * @param plcaddresses
     */
    public void Init(List<Res_PLCADDRESS1> plcaddresses,Activity activity,boolean Issd)
    {
        for (Res_PLCADDRESS1 item:plcaddresses)
        {
            Switch aSwitch=new Switch(activity);
            AutoNextLineLinearLayout.LayoutParams params = new AutoNextLineLinearLayout
                    .LayoutParams(AutoNextLineLinearLayout.LayoutParams.WRAP_CONTENT,
                    AutoNextLineLinearLayout.LayoutParams.WRAP_CONTENT);
            //通过这个属性可以控制 排列方式
            params.setMargins(20, 10, 0, 0);
            aSwitch.setLayoutParams(params);
            //aSwitch.setWidth(120);
            aSwitch.setText(item.name);
            if(Issd)
            {
                aSwitch.setTag(item.address);
               // aSwitch.setChecked(ExecuteTheRecipe.ReadPLCSD(item.address));
            }else
            {
                aSwitch.setTag(item.zd_address);
               // aSwitch.setChecked(ExecuteTheRecipe.ReadPLCSD(item.address));
            }
            aSwitch.setPadding(30,0,0,0);
            aSwitch.setHeight(105);
            aSwitch.setTextSize(16);
            aSwitch.setTextColor(ConfigName.getInstance().dishesCon.getResources().getColor(R.color.white));
            aSwitch.setBackground(ConfigName.getInstance().dishesCon.getResources().getDrawable(R.drawable.bg_btn_login_selected));
            aSwitch.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    boolean isopen= aSwitch.isChecked();
                    String addr=  aSwitch.getTag().toString();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                ExecuteTheRecipe.WritePLC("手自切换",false,null);
                                ExecuteTheRecipe.WritePLCSD(addr,isopen,null);
                            } catch (Exception e) {

                            }
                        }
                    }).start();
                    if(isopen)
                    {
                        aSwitch.setBackground(ConfigName.getInstance().dishesCon.getResources().getDrawable(R.drawable.button1));
                    }else
                    {
                        aSwitch.setBackground(ConfigName.getInstance().dishesCon.getResources().getDrawable(R.drawable.bg_btn_login_selected));
                    }
                    ToastUtils.info(aSwitch.getText().toString()+",地址:"+addr+".状态:"+isopen);
                }
            });
            tagLayout.addView(aSwitch);
        }
    }
}
