package com.example.iot_controlhost.adapter;

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

import com.example.iot_controlhost.R;
import com.example.iot_controlhost.base.BaseRecyclerAdapter;
import com.example.iot_controlhost.databinding.ItemUvTaskBinding;
import com.example.iot_controlhost.model.MyTask;
import com.example.iot_controlhost.ui.dialog.ConfirmDialog;
import com.example.iot_controlhost.utils.timer.UVTaskUtil;
import com.example.iot_controlhost.utils.log.UserLog;

import java.util.List;

/**
 * @Author：DuanKaiji
 * @Date：2023年11月27日 14:01:37
 * @Description：消毒任务适配器
 */
public class UvTaskAdapter extends BaseRecyclerAdapter<MyTask, ItemUvTaskBinding> {
    Context mContext;

    public UvTaskAdapter(List<MyTask> dataList, Class<ItemUvTaskBinding> bindingClass, View tvTipNone) {
        super(dataList, bindingClass, tvTipNone);
    }

    public void setContext(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    protected void bindData(ItemUvTaskBinding binding, MyTask data) {
        binding.tvStartTime.setText(data.getStartTimeHour() + ":" + data.getStartTimeMinute());
        binding.tvEndTime.setText(data.getEndTimeHour() + ":" + data.getEndTimeMinute());
        binding.btnDelete.setOnClickListener(view -> new ConfirmDialog(mContext, mContext.getString(R.string.warning), mContext.getString(R.string.ask_delete_plan), () -> {
            //删除消毒任务
            UVTaskUtil.removeUvTask(data);
            dataList.remove(data);
            notifyDataSetChanged();
            UserLog.operate("本机屏幕", String.format("删除消毒计划成功：%02d:%02d→%02d:%02d",
                    data.getStartTimeHour(), data.getStartTimeMinute(),
                    data.getEndTimeHour(), data.getEndTimeMinute()));
        }).show());
    }

    @Override
    protected void bindData(ItemUvTaskBinding binding, MyTask data, int position) {
        super.bindData(binding, data, position);
        binding.tvId.setText(String.valueOf(position + 1));
    }

    @Override
    public int getItemCount() {
        if (dataList == null) {
            return 0;
        } else {
            return dataList.size();
        }
    }

}
