package com.casic.dcms.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.casic.dcms.R;
import com.casic.dcms.model.LightListBean;

import java.util.List;

public class LightResultAdapter extends RecyclerView.Adapter<LightResultAdapter.ItemViewHolder> {

    private final List<LightListBean.DataBean.RowsBean> dataBeans;
    private final LayoutInflater layoutInflater;

    public LightResultAdapter(Context context, List<LightListBean.DataBean.RowsBean> beans) {
        this.dataBeans = beans;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(layoutInflater.inflate(R.layout.item_light_recycleview, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull LightResultAdapter.ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.bindView(dataBeans.get(position));
        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onClick(position);
                }
            });
        }
        if (switchClickListener != null) {
            holder.lightSwitch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switchClickListener.onClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }

    private OnGridItemClickListener clickListener;

    public interface OnGridItemClickListener {
        void onClick(int position);
    }

    public void setOnGridItemClickListener(OnGridItemClickListener onGridItemClickListener) {
        this.clickListener = onGridItemClickListener;
    }

    private OnSwitchClickListener switchClickListener;

    public interface OnSwitchClickListener {
        void onClick(int position);
    }

    public void setOnSwitchClickListener(OnSwitchClickListener switchClickListener) {
        this.switchClickListener = switchClickListener;
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {

        private final TextView lightNameView;
        private final TextView lightStatusView;
        //        private ImageView lightStatusImageView;
        private final ImageView lightSwitch;

        ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            lightNameView = itemView.findViewById(R.id.lightNameView);
            lightStatusView = itemView.findViewById(R.id.lightStatusView);
//            lightStatusImageView = itemView.findViewById(R.id.lightStatusImageView);
            lightSwitch = itemView.findViewById(R.id.lightSwitch);
        }

        void bindView(LightListBean.DataBean.RowsBean dataBean) {
            lightNameView.setText(dataBean.getLampName());
            String status = dataBean.getStatus();
            String statusName = dataBean.getStatusName();
            lightStatusView.setText(statusName);
            if (statusName.equals("离线")) {
//                lightStatusImageView.setBackgroundResource(R.drawable.ic_streetlight_icon_off);
                lightSwitch.setBackgroundResource(R.drawable.ic_switch_off);
            } else if (statusName.equals("报警")) {
                lightSwitch.setBackgroundResource(R.drawable.ic_switch_warning);
            } else {
//                lightStatusImageView.setBackgroundResource(R.drawable.ic_streetlight_icon_on);
                lightSwitch.setBackgroundResource(R.drawable.ic_switch_on);
            }
        }
    }
}
