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.R;
import com.rdiot.yx485.bean.HomeDefBean;
import com.rdiot.yx485.databinding.ItemListTopBinding;
import com.rdiot.yx485.http.Url;
import com.rdiot.yx485.webview.WebViewActivity;

import java.util.List;

public class TopDeviceAdapter extends RecyclerView.Adapter<TopDeviceAdapter.DeviceViewHolder> {
    private OnItemClickListener listener;
    private List<HomeDefBean.DataBean.RoomBean.DevicesBean> devices;
    private Context mContext;

    public TopDeviceAdapter(List<HomeDefBean.DataBean.RoomBean.DevicesBean> devices, OnItemClickListener listener) {
        this.devices = devices;
        this.listener = listener;
    }

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

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

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

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

    public class DeviceViewHolder extends RecyclerView.ViewHolder {
        ItemListTopBinding mBinding;

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

        public void bind(int position) {
            HomeDefBean.DataBean.RoomBean.DevicesBean device = devices.get(position);
            if (device == null) {
                return;
            }

            if (!TextUtils.isEmpty(device.icon)) {
                Glide.with(mContext)
                        .load(Url.BASE_URL + device.icon)
                        .error(Glide.with(mContext).load(R.drawable.find_d))
                        .into(mBinding.image);
                // holder.deviceIcon.setImageResource(device.icon);
            }

            mBinding.name.setText(device.name);
            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 (listener != null) {
                        listener.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);
                }
            });
        }
    }
}