package com.rdiot.yx485.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.rdiot.yx485.bean.HomeDefBean;
import com.rdiot.yx485.databinding.ItemGridelistTopBinding;
import com.rdiot.yx485.http.Url;
import com.rdiot.yx485.webview.WebViewActivity;

import java.util.List;

public class TopDevicegridAdapter extends RecyclerView.Adapter<TopDevicegridAdapter.DeviceViewHolder> {

    private final List<HomeDefBean.DataBean.RoomBean.DevicesBean> devices;
    OnItemCheckedListener onItemCheckedListener;
    private Context mContext;

    public TopDevicegridAdapter(List<HomeDefBean.DataBean.RoomBean.DevicesBean> devices, OnItemCheckedListener listener) {
        this.devices = devices;
        this.onItemCheckedListener = listener;
    }

    @NonNull
    @Override
    public DeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        ItemGridelistTopBinding binding = ItemGridelistTopBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new DeviceViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        holder.bindView(position);
    }

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

    public interface OnItemCheckedListener {
        void onChecked(int position, Boolean isChecked);
    }

    public class DeviceViewHolder extends RecyclerView.ViewHolder {
        ItemGridelistTopBinding mBinding;

        public DeviceViewHolder(@NonNull ItemGridelistTopBinding itemView) {
            super(itemView.getRoot());
            mBinding = itemView;
        }

        public void bindView(int position) {
            HomeDefBean.DataBean.RoomBean.DevicesBean device = devices.get(position);
            mBinding.name.setText(device.name);
            mBinding.stae.setText(device.temperature + "℃｜" + device.humidity + "%");
            if (device.icon != null) {
                Glide.with(mContext)
                        .load(Url.BASE_URL + device.icon)
                        .into(mBinding.image);
                // holder.deviceIcon.setImageResource(device.icon);
            }

            if (device.show_online) {
                mBinding.stae.setText(device.is_online ? "设备在线" : "设备离线");
            } else {
                mBinding.stae.setText(String.format("%s℃ ｜%s%%", device.temperature, device.humidity));
            }

            if (device.show_power) {
                mBinding.toggle.setVisibility(View.VISIBLE);
                mBinding.toggle.setChecked(device.power == 1);
                mBinding.toggle.setOnCheckedChangeListener((compoundButton, b) -> {
                    if (onItemCheckedListener != null) {
                        onItemCheckedListener.onChecked(position, b);
                    }
                });
            } else {
                mBinding.toggle.setVisibility(View.GONE);
            }

            mBinding.getRoot().setOnClickListener(v -> {
                if (!TextUtils.isEmpty(device.detail_url)) {
                    WebViewActivity.newIntent(mContext, device.detail_url, null);
                }
            });
        }
    }
}