package com.rdiot.yx485.adapter;


import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.king.zxing.util.LogUtils;
import com.rdiot.yx485.R;
import com.rdiot.yx485.bean.ModelRepleBean;
import com.rdiot.yx485.http.Url;
import com.rdiot.yx485.webview.WebViewActivity;
import com.zcolin.gui.zrecyclerview.BaseRecyclerAdapter;


public class ExampleListAdapter extends BaseRecyclerAdapter<ModelRepleBean.DataBean> {

    private final Context mContext;
    private final OnItemClickListener mClickListener;
    private boolean mShowAction = false;

    public ExampleListAdapter(Context context, OnItemClickListener listener, boolean showAction) {
        mContext = context;
        mClickListener = listener;
        mShowAction = showAction;
    }

    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.view_example_item;
    }

    @Override
    public void setUpData(CommonHolder holder, int position, int viewType, ModelRepleBean.DataBean data) {
        holder.setIsRecyclable(false);

        TextView name = getView(holder, R.id.name);
        TextView desc = getView(holder, R.id.tv_desc);
        ImageView imageView = getView(holder, R.id.image);
        ImageView smallIcon = getView(holder, R.id.iv_small_icon);
        ImageView arrowRight = getView(holder, R.id.iv_arrow_right);
        RecyclerView rvDeviceListView = getView(holder, R.id.rv_device_list);
        rvDeviceListView.setLayoutManager(new LinearLayoutManager(mContext));

        ToggleButton toggle = getView(holder, R.id.btn_toggle);
        toggle.setVisibility(mShowAction ? GONE : VISIBLE);
        TextView action = getView(holder, R.id.btn_action);
        action.setVisibility(mShowAction ? VISIBLE : GONE);

        if (!TextUtils.isEmpty(data.icon)) {
            Glide.with(holder.itemView.getContext())
                    .load(Url.BASE_URL + data.icon)
                    .error(Glide.with(holder.itemView.getContext()).load(R.drawable.find_d))
                    .into(imageView);
            // holder.deviceIcon.setImageResource(device.icon);
        }

        if (!TextUtils.isEmpty(data.name)) {
            name.setText(data.name);
        }

        if (!TextUtils.isEmpty(data.small_icon)) {
            Glide.with(holder.itemView.getContext())
                    .load(Url.BASE_URL + data.small_icon)
                    .error(Glide.with(holder.itemView.getContext()).load(R.drawable.find_d))
                    .into(smallIcon);
        }

        toggle.setChecked(data.status);
        toggle.setOnCheckedChangeListener((compoundButton, b) -> {
            LogUtils.d("checked:" + b);
            if (mClickListener != null) {
                mClickListener.onItemClick(data.record_id);
            }
        });
        action.setOnClickListener(v -> {
            if (mClickListener != null) {
                mClickListener.onItemClick(data.record_id);
            }
        });

        if (!TextUtils.isEmpty(data.detail_url)) {
            holder.itemView.setOnClickListener(v -> WebViewActivity.newIntent(mContext, data.detail_url, null));
        }

        if (data.device_infos == null) {
            desc.setText("相关设备已离线");
            smallIcon.setVisibility(GONE);
            arrowRight.setVisibility(GONE);
        } else {
            for (ModelRepleBean.DataBean.DeviceInfosBean infosBean : data.device_infos) {
                if (infosBean.online) {
                    continue;
                }

                desc.setText("相关设备已离线");
                smallIcon.setVisibility(GONE);
                arrowRight.setVisibility(GONE);
                return;
            }

            DeviceIconListAdapter adapter = new DeviceIconListAdapter(data.device_infos, mContext);
            rvDeviceListView.setAdapter(adapter);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(String id);
    }
}