package com.sybotan.android.demo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.framework.mvvm.model.db.entity.ObjectEntity;
import com.framework.mvvm.model.db.entity.Options;
import com.sybotan.android.demo.R;
import com.sybotan.android.demo.retrofit.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cunwen on 16/6/21.
 */
public class SelectPopListAdapter extends RecyclerView.Adapter<SelectPopListAdapter.BuildViewHolder> {
    private List<?> items = new ArrayList<>();
    private Context context;
    private int selectPosition;
    private String type;
    private boolean isRed;
    private int redPosition;
    private Boolean flag;

    public void setItems(List<?> items, Context context, int position, String type) {
        this.items = items;
        this.context = context;
        this.selectPosition = position;
        this.type = type;
    }


    @Override
    public BuildViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mRootView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.personal_select_text_item, parent, false);
        return new BuildViewHolder(mRootView);
    }

    @Override
    public void onBindViewHolder(final BuildViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        final Object item = items.get(position);
        if (type.equals(Constant.TYPE1)) {
            Options options = (Options) item;
            holder.choice_name.setText(options.getTxt());
        }
        if (type.equals(Constant.TYPE2)) {
            ObjectEntity options = (ObjectEntity) item;
            if (TextUtils.isEmpty(options.getLocalId())) {
                holder.choice_name.setText(options.getLocalName());
            } else {
                holder.choice_name.setText(options.getLocalId());
            }
        }
        if (selectPosition > 0 && selectPosition - 1 == position) {
            holder.choice_name.setTextColor(context.getResources().getColor(R.color.blue_2f48b7));
        } else {
            holder.choice_name.setTextColor(context.getResources().getColor(R.color.black_272727));
        }
        if (isRed && position == redPosition) {
            holder.choice_name.setTextColor(context.getResources().getColor(R.color.red_D44D44));
        } else if (isRed) {
            holder.choice_name.setTextColor(context.getResources().getColor(R.color.black_272727));
        }
        holder.radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(holder.choice_layout, position, isChecked);
                }
            }
        });
//        holder.choice_layout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mOnItemClickListener != null) {
//                    mOnItemClickListener.onItemClick(holder.choice_layout, position);
//                }
//            }
//        });
//        if (flag) {
//            holder.choice_name.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
//        } else {
        holder.choice_name.setGravity(Gravity.CENTER);
//        }
    }

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

    public void setRedText(int i, boolean isRed) {
        this.isRed = true;
        this.redPosition = i;
        notifyDataSetChanged();
    }

    public void setFlag(Boolean flag) {
        this.flag = flag;
        notifyDataSetChanged();
    }


    static class BuildViewHolder extends BaseViewHolder {
        TextView choice_name;
        LinearLayout choice_layout;
        CheckBox radioButton;

        public BuildViewHolder(View mRootView) {
            super(mRootView);
            choice_name = (TextView) mRootView.findViewById(R.id.choice_name);
            choice_layout = (LinearLayout) mRootView.findViewById(R.id.choice_layout);
            radioButton = (CheckBox) mRootView.findViewById(R.id.radioButton);

        }
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, Integer position, boolean isChecked);
    }
}
