package com.sjkj.appthreefloor_tsgz.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.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

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

import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.bean.CommonDateBean;

import java.util.List;


/**
 * Created by lenovo on 2017/12/8.
 */

public class RecycleviewRightAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private OnItemClickListener mOnItemClickListener = null;
    private List<CommonDateBean> mDatas;
    private Context mContext;
    private LayoutInflater inflater;
    public static OnResourcesClick onResourcesClick;
    private int checkedPosition = -1;
    public static final int TYPE_ITEM_ONE = 1;
    public static final int TYPE_ITEM_TWO = 2;

    public static interface OnItemClickListener {
        void onItemClick(CommonDateBean bean, int position, int type);
    }

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

    public RecycleviewRightAdapter(Context context, List<CommonDateBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemViewType(int position) {

        return mDatas.get(position).getType();
    }

    public void refush(List<CommonDateBean> datas) {
        this.mDatas = datas;
        notifyDataSetChanged();
    }

    /**
     * 设置列表的点击事件回调接口
     */
    public static void setOnResourcesClick(OnResourcesClick onResourcesClick) {
        RecycleviewRightAdapter.onResourcesClick = onResourcesClick;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_ITEM_ONE:
                return new MyViewHolder(inflater.inflate(R.layout.item_classify_right_ziyuan, parent, false));
            case TYPE_ITEM_TWO:
                return new MyViewHolder2(inflater.inflate(R.layout.item_classify_right_ziyuan2, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (holder instanceof MyViewHolder) {

            MyViewHolder myViewHolder = ((MyViewHolder) holder);
            myViewHolder.txt_tit.setText(mDatas.get(position).getName() + "km");
            if (position == checkedPosition) {
                ((MyViewHolder) holder).iv_checkbox.setVisibility(View.VISIBLE);
                ((MyViewHolder) holder).txt_tit.setTextColor(mContext.getResources().getColor(R.color.event_color));
            } else {
                ((MyViewHolder) holder).iv_checkbox.setVisibility(View.GONE);
                ((MyViewHolder) holder).txt_tit.setTextColor(mContext.getResources().getColor(R.color.recyclerview_lefttext_select));
            }
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(mDatas.get(position), position, TYPE_ITEM_ONE);

                }
            });

        } else {
            MyViewHolder2 myViewHolder2 = ((MyViewHolder2) holder);
            myViewHolder2.txt_tit.setText(mDatas.get(position).getName());
            myViewHolder2.tv_distance.setText("距 离:" + convertMetersToKilometers(mDatas.get(position).getDistance()) + "km");
            myViewHolder2.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(mDatas.get(position), position, TYPE_ITEM_TWO);

                }
            });
        }

    }
    public static String convertMetersToKilometers(double meters) {
        double kilometers = meters / 1000;
        return String.format("%.2f", kilometers); // 保留一位小数
    }

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


    //得到当前选中的位置
    public int getCheckedPosition() {
        return checkedPosition;
    }

    public void clear() {
        // map.clear();
        checkedPosition = -1;
    }

    public void setCheckedPosition(int position) {
        checkedPosition = position;
        notifyDataSetChanged(); // 通知适配器数据已改变，需要重新绑定视图
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView txt_tit;
        ImageView iv_checkbox;
        LinearLayout itemView;

        public MyViewHolder(View view) {
            super(view);
            txt_tit = (TextView) view.findViewById(R.id.text);//标题
            iv_checkbox = (ImageView) view.findViewById(R.id.iv_checkbox);//图标
            itemView = (LinearLayout) view.findViewById(R.id.item_layout);

        }
    }

    static class MyViewHolder2 extends RecyclerView.ViewHolder {

        TextView txt_tit;
        TextView tv_distance;
        ImageView iv_next;
        RelativeLayout itemView;

        public MyViewHolder2(View view) {
            super(view);
            txt_tit = (TextView) view.findViewById(R.id.text);//标题
            tv_distance = (TextView) view.findViewById(R.id.tv_distance);//距离
            iv_next = (ImageView) view.findViewById(R.id.iv_next);//图标
            itemView = (RelativeLayout) view.findViewById(R.id.item_layout);

        }
    }
}
