package com.example.iot_controlhost.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import com.example.iot_controlhost.R;
import com.example.iot_controlhost.base.BaseRecyclerAdapter;
import com.example.iot_controlhost.databinding.ItemControllerBinding;
import com.example.iot_controlhost.model.Result;
import com.example.iot_controlhost.model.controller.relay.Relay;
import com.example.iot_controlhost.model.sensor.GasSensor;
import com.example.iot_controlhost.model.thread.QueueIOUITask;
import com.example.iot_controlhost.utils.DialogUtil;
import com.example.iot_controlhost.utils.MyQueue;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.global.RoomSensor;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.log.UserLog;

import java.util.List;

import es.dmoral.toasty.Toasty;

/**
 * 手动控制继电器下的设备
 * 用于主界面显示
 *
 * @Author：DuanKaiji
 */
public class ManualControllerAdapter extends BaseRecyclerAdapter<Relay, ItemControllerBinding> {
    Context mContext;

    public ManualControllerAdapter(Context context, List<Relay> dataList, Class<ItemControllerBinding> bindingClass) {
        super(dataList, bindingClass);
        this.mContext = context;
    }

    @Override
    protected void bindData(ItemControllerBinding binding, Relay device) {
        if (device.getName().equals(RoomController.fan.getName())) {
            binding.tvInfo.setText(GasSensor.getQualityAir());
            binding.tvInfo.setTextColor(GasSensor.getQualityAirColor());
        } else {
            binding.tvInfo.setText(null);
        }
        changeState(binding, device);
        binding.tvController.setText(device.getName());
        binding.cardController.setOnClickListener(view -> {
            DialogUtil.showLoadingDialog(mContext, mContext.getString(R.string.in_operate));
            MyQueue.start(MyQueue.TYPE_CONTROLLER, new QueueIOUITask<Result>() {
                @Override
                public Result doInQueueThread() throws Exception {
                    Result result = new Result();
                    result.setOperateResult(device.setPowerSupply(!device.isPowerSupply()));
                    result.setState(device.isPowerSupply());
                    return result;
                }

                @Override
                public void doInUIThread(Result result) {
                    if (result.isOperateResult()) {
                        changeState(binding, device);
                        Toasty.success(mContext, mContext.getString(R.string.operate_success)).show();
                        UserLog.operate("手动：" + (result.isState() ? "开启" : "关闭") + device.getName());
                    } else {
                        Toasty.error(mContext, mContext.getString(R.string.operate_fail)).show();
                        UserLog.operateError("手动操作失败：" + (result.isState() ? "开启" : "关闭") + device.getName());
                    }
                    DialogUtil.hideLoadingDialog();
                }
            });
        });
    }

    private void changeState(ItemControllerBinding binding, Relay device) {
        if ("未配置".equals(device.getName())) {
            binding.cardNo.setVisibility(View.VISIBLE);
            binding.cardYes.setVisibility(View.GONE);
            return;
        } else {
            binding.cardNo.setVisibility(View.GONE);
            binding.cardYes.setVisibility(View.VISIBLE);
        }
        if (device.isPowerSupply()) {
            binding.tvState.setText("运行中");
            binding.ivController.setImageResource(device.getIconOpen());
            binding.ivPower.setBackground(mContext.getDrawable(R.drawable.bg_controller_open));
            binding.ivPower.setImageDrawable(mContext.getDrawable(R.mipmap.switch_on));
            binding.cardController.setBackgroundColor(Color.parseColor("#00BDF4"));
            binding.ivController.setImageDrawable(mContext.getDrawable(device.getIconOpen()));
            binding.tvState.setTextColor(Color.parseColor("#FFFFFF"));
            binding.tvController.setTextColor(Color.parseColor("#FFFFFF"));
            binding.tvPower.setTextColor(Color.parseColor("#FFFFFF"));
        } else {
            binding.tvState.setText("未启动");
            binding.ivController.setImageResource(device.getIconClose());
            binding.cardController.setBackgroundColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#33DCE3E3" : "#F2F0F1"));
            binding.ivPower.setBackground(mContext.getDrawable(R.drawable.bg_controller_close));
            binding.ivPower.setImageDrawable(mContext.getDrawable(R.mipmap.switch_off));
            binding.ivController.setImageDrawable(mContext.getDrawable(device.getIconClose()));
            binding.tvState.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
            binding.tvController.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
            binding.tvPower.setTextColor(Color.parseColor(RoomSetting.isDarkTheme() ? "#FFFFFF" : "#3A3A3A"));
        }
        binding.tvPower.setVisibility(RoomController.relay.getModel().equals(RoomController.relay.getModels()[2]) ? View.VISIBLE : View.GONE);
        binding.tvPower.setText("实时功率：" + device.getPower() + "w");
    }

}
