package com.tofly.ghrq_route.common.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.esri.arcgisruntime.data.Feature;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.map.service.Container;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/1/10.
 */

public class PipenetResultAdapter extends RecyclerView.Adapter {
    private static final int MAX_SIZE = 100;
    private List<Feature> featuerResultList;
    private Context context;
    private int selectedPostion = -1;
    private Container.OnResultClickListener onResultClickListener;

    public PipenetResultAdapter(List<Feature> featuertList, Context context) {
        this.featuerResultList = featuertList;
        this.context = context;
    }

    public void setDataChanged(List<Feature> featuertList, int selectedPosition) {
        this.selectedPostion = selectedPosition;
        featuerResultList = featuertList;
        notifyDataSetChanged();
    }

    public void clearList() {
        featuerResultList.clear();
        notifyDataSetChanged();
    }

    public void setSelected(int postion) {
        selectedPostion = postion;
//        notifyItemChanged(postion);
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout
                .item_fragment_pipenet, parent,false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setData(position);

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        int size = featuerResultList.size();
        if (size > MAX_SIZE) {
            ToastUtil.showToast(context,"只显示前"+ MAX_SIZE +"条数据");
        }
        return size > MAX_SIZE ? MAX_SIZE :size;
    }

    public void setResultListener(Container.OnResultClickListener onResultClickListener) {
        this.onResultClickListener = onResultClickListener;
    }


    protected class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.pipenet_num)
        TextView pipenetNum;
        @BindView(R.id.pipenet_design_press)
        TextView pipenetDesignPress;
        @BindView(R.id.pipenet_material)
        TextView pipenetMaterial;
        @BindView(R.id.pipenet_radius)
        TextView pipenetRadius;
        @BindView(R.id.pipenet_address)
        TextView pipenetAddress;
        @BindView(R.id.pipenet_project_name)
        TextView pipenetProjectName;
        @BindView(R.id.layout_header_row)
        LinearLayout layoutHeaderRow;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(final int position) {
            if (position % 2 == 1) {
                this.itemView.setBackgroundResource(R.color.scada_shadow);
            } else {
                this.itemView.setBackgroundResource(R.color.white);
            }
            int colorText = context.getResources().getColor(R.color.text);
            this.pipenetNum.setTextColor(colorText);
            this.pipenetDesignPress.setTextColor(colorText);
            this.pipenetMaterial.setTextColor(colorText);
            this.pipenetRadius.setTextColor(colorText);
            this.pipenetAddress.setTextColor(colorText);
            this.pipenetProjectName.setTextColor(colorText);

            if (selectedPostion >= 0) {
                if (position == selectedPostion) {
                    this.itemView.setBackgroundResource(R.color.bg_gray);
                    int colorWhite = context.getResources().getColor(R.color.white);
                    this.pipenetNum.setTextColor(colorWhite);
                    this.pipenetDesignPress.setTextColor(colorWhite);
                    this.pipenetMaterial.setTextColor(colorWhite);
                    this.pipenetRadius.setTextColor(colorWhite);
                    this.pipenetAddress.setTextColor(colorWhite);
                    this.pipenetProjectName.setTextColor(colorWhite);
                }
            }

            this.pipenetNum.setText(String.valueOf(position + 1));
            final Feature feature = featuerResultList.get(position);
            if (feature != null) {
                Map<String, Object> attributes = feature.getAttributes();
                if (attributes.containsKey("DESPRESSURE")) {
                    Object object = attributes.get("DESPRESSURE");
                    String designPress = "null";
                    if (object != null) {
                        designPress = object.toString();
                    }
                    this.pipenetDesignPress.setText(designPress);
                }
                if (attributes.containsKey("MATERIAL")) {
                    Object object = attributes.get("MATERIAL");
                    String material = "null";
                    if (object != null) {
                        material = object.toString();
                    }
                    this.pipenetMaterial.setText(material);
                }
                if (attributes.containsKey("DIAMETER")) {
                    Object object = attributes.get("DIAMETER");
                    String radius = "null";
                    if (object != null) {
                        radius = object.toString();
                    }
                    this.pipenetRadius.setText(radius);
                }
                if (attributes.containsKey("ADDR")) {
                    Object object = attributes.get("ADDR");
                    String address = "null";
                    if (object != null) {
                        address = object.toString();
                    }
                    this.pipenetAddress.setText(address);
                }
                if (attributes.containsKey("DRAWNAME")) {
                    Object object = attributes.get("DRAWNAME");
                    String projectName = "null";
                    if (object != null) {
                        projectName = object.toString();
                    }
                    this.pipenetProjectName.setText(projectName);
                }
            }
            this.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onResultClickListener != null) {
                        onResultClickListener.showCalloutAtMap(feature,position);
                    }
                }
            });
        }
    }
}
