package com.example.bpa.view.control;

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 android.widget.TextView;

import androidx.annotation.Nullable;

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.from.lcsz_activity;

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_PLCADDRESS> 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_PLCADDRESS> plcaddresses,Activity activity,boolean Issd)
    {
        for (Res_PLCADDRESS item:plcaddresses)
        {
            Switch aSwitch=new Switch(activity);
            AutoNextLineLinearLayout.LayoutParams params = new AutoNextLineLinearLayout
                    .LayoutParams(AutoNextLineLinearLayout.LayoutParams.WRAP_CONTENT,
                    AutoNextLineLinearLayout.LayoutParams.WRAP_CONTENT);
            //通过这个属性可以控制 排列方式
            params.setMargins(20, 0, 0, 0);
            aSwitch.setLayoutParams(params);
            //aSwitch.setWidth(120);
            aSwitch.setText(item.name);
            if(Issd)
            {
                aSwitch.setTag(item.address);
                aSwitch.setChecked(DeviceData.Get().getManualValue(item.address));
            }else
            {
                aSwitch.setTag(item.zd_address);
                aSwitch.setChecked(DeviceData.Get().getAutomationValue(item.address));
            }

            aSwitch.setBackground(ConfigName.getInstance().dishesCon.getResources().getDrawable(R.drawable.qs_switch));
            aSwitch.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    boolean isopen= aSwitch.isChecked();
                    String addr=  aSwitch.getTag().toString();
                    DeviceData.Get().setSwitch(isopen,addr,null);
                    T.show(view.getContext(), aSwitch.getText().toString()+",地址:"+addr+".状态:"+isopen);
                }
            });
            tagLayout.addView(aSwitch);
        }
    }
}