package com.bonait.bnframework.modules.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
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.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.res.StatusMode;
import com.bonait.bnframework.common.db.res.lcMode;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.textview.QMUILinkTextView;

import java.util.List;

public class monitor_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;

    private Context context;

    List<StatusMode> statusModes = ConfigName.getInstance().statusModes;

    public monitor_adapter(Context context) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.devstatus, parent, false);
        return new MyLCViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof MyLCViewHolder) {
                MyLCViewHolder myViewHolder = (MyLCViewHolder) holder;
                StatusMode statusMode= statusModes.get(position);
                myViewHolder.title.setText(statusMode.Name);
                if(statusMode.Status)
                {
                    myViewHolder.image.setImageResource(statusMode.Url_open);
                    myViewHolder.text.setText("异常");
                    myViewHolder.text.setTextColor(context.getResources().getColor(R.color.red_primary));
                }else
                {
                    myViewHolder.image.setImageResource(statusMode.Url_close);
                    myViewHolder.text.setText("正常");
                    myViewHolder.text.setTextColor(context.getResources().getColor(R.color.green_primary));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Activity findActivity(@NonNull Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return findActivity(((ContextWrapper) context).getBaseContext());
        } else {
            return null;
        }
    }

    /**
     * 刷新
     *
     * @param
     */
    public void refresh() {
        Activity activity = findActivity(context);
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        notifyDataSetChanged();
                    } catch (Exception e) {

                    }
                }
            });
        }
    }

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

    public static class MyLCViewHolder extends RecyclerView.ViewHolder {
        /**
         * 标题
         */
        TextView title;
         ImageView image;
        /**
         * 描述
         */
        TextView text;
        public MyLCViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            text=(TextView) view.findViewById(R.id.text);
            image= view.findViewById(R.id.image);
        }
    }
}
