package com.example.iot_controlhost.adapter;

import com.example.iot_controlhost.R;
import com.example.iot_controlhost.base.BaseRecyclerAdapter;
import com.example.iot_controlhost.databinding.DialogMessageBinding;
import com.example.iot_controlhost.databinding.ItemMsgTitleBinding;
import com.example.iot_controlhost.model.Log;
import com.example.iot_controlhost.utils.database.MessageReadUtil;
import com.example.iot_controlhost.utils.global.RoomSetting;

import java.util.List;

/**
 * 日志适配器
 * 用于查找日志弹窗
 *
 * @Author：DuanKaiji
 */
public class MessageAdapter extends BaseRecyclerAdapter<Log, ItemMsgTitleBinding> {

    DialogMessageBinding dialogBinding;

    public MessageAdapter(List<Log> dataList, Class<ItemMsgTitleBinding> bindingClass, DialogMessageBinding binding) {
        super(dataList, bindingClass);
        this.dialogBinding = binding;
    }

    @Override
    protected void bindData(ItemMsgTitleBinding binding, Log data) {
        int targetColorId = binding.tvTitle.getContext().getColor(
                data.getLevel() == android.util.Log.ERROR ? R.color.red : RoomSetting.isDarkTheme() ? R.color.white : R.color.black);
        binding.tvTitle.setTextColor(targetColorId);
        // 如果未读 字体加粗
        binding.tvTitle.setTypeface(null, MessageReadUtil.INSTANCE.isRead(data.getId().toString()) ? android.graphics.Typeface.NORMAL : android.graphics.Typeface.BOLD);
        binding.linearLayout6.setOnClickListener(v ->{
            // 点击时，显示日志内容
            dialogBinding.tvMsgTitle.setText(data.getTag());
            dialogBinding.tvMsgTime.setText(data.getDatetime().toString());
            dialogBinding.tvMessage.setText(data.getMessage());

            // 标记为已读
            MessageReadUtil.INSTANCE.readMessage(data.getId().toString());// todo
            binding.tvTitle.setTypeface(null, android.graphics.Typeface.NORMAL);
        });
    }
}
