package com.navinfo.outdoor.adapter;

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

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

import com.navinfo.outdoor.R;
import com.navinfo.outdoor.room.PoiEntity;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 筛选的适配器
 */
public class FilterAdapter extends RecyclerView.Adapter<FilterAdapter.ViewHolder> {
    private Context context;
    private List<PoiEntity> allPoi;

    public FilterAdapter(Context context, List<PoiEntity> allPoi) {
        this.context = context;
        this.allPoi = allPoi;
    }


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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PoiEntity poiEntity = allPoi.get(position);
        holder.tvName.setText(poiEntity.getName());
        if (poiEntity.getTaskId() != 0) {
            holder.tvTaskId.setText("任务id:" + poiEntity.getTaskId());
        } else {
            holder.tvTaskId.setText("任务id:" + 0);
        }

        if (poiEntity.getPrecision() != null) {
            holder.tvMoney.setText("￥" + format2(Double.valueOf(poiEntity.getPrecision())));
        } else {
            holder.tvMoney.setText("￥" + 0);
        }
        if (poiEntity.getDist() != null) {
            holder.tvDistance.setText("距离：" + format2(Double.valueOf(poiEntity.getDist()) / 1000) + "km");
        } else {
            holder.tvDistance.setText("距离：" + 0);
        }
        //0.未领取 1.已领取，2.未保存(保存到本地但未提交成功),3.已保存(保存到本地提交成功)，4已上传（结束采集）,
        if (poiEntity.getTaskStatus() == 0) {//"未领取", "已领取", "未保存", "已保存"
            holder.tvTaskStatus.setVisibility(View.GONE);
            holder.tvTaskStatus.setText("未领取");
        } else if (poiEntity.getTaskStatus() == 1) {
            holder.tvTaskStatus.setVisibility(View.VISIBLE);
            holder.tvTaskStatus.setText("已领取");
            holder.tvTaskStatus.setTextColor(Color.parseColor("#FFC107"));
        } else if (poiEntity.getTaskStatus() == 2) {
            holder.tvTaskStatus.setVisibility(View.VISIBLE);
            holder.tvTaskStatus.setText("未保存");
            holder.tvTaskStatus.setTextColor(Color.parseColor("#1CEF7B"));
        }/*else if (poiEntity.getTaskStatus()==3){
            holder.tvTaskStatus.setVisibility(View.VISIBLE);
            holder.tvTaskStatus.setText("已保存");
            holder.tvTaskStatus.setTextColor(Color.parseColor("#00BCD4"));
        }else if (poiEntity.getTaskStatus()==4){
            holder.tvTaskStatus.setVisibility(View.VISIBLE);
            holder.tvTaskStatus.setText("已上传");
        }*/
        if (poiEntity.getType() == 1) {
            holder.tvForm.setText("poi");
        } else if (poiEntity.getType() == 2) {
            holder.tvForm.setText("充电站");
        } else if (poiEntity.getType() == 3) {
            holder.tvForm.setText("POI录像");
        } else if (poiEntity.getType() == 4) {
            holder.tvForm.setText("道路录像");
        } else if (poiEntity.getType() == 5) {
            holder.tvForm.setText("其他");
        } else if (poiEntity.getType() == 6) {
            holder.tvForm.setText("面状任务");
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemCLick != null) {
                    itemCLick.item(poiEntity);
                }
            }
        });


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName, tvForm, tvTaskId, tvMoney, tvDistance, tvTaskStatus;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            tvForm = itemView.findViewById(R.id.tv_form);
            tvTaskId = itemView.findViewById(R.id.tv_task_id);
            tvMoney = itemView.findViewById(R.id.tv_money);
            tvDistance = itemView.findViewById(R.id.tv_distance);
            tvTaskStatus = itemView.findViewById(R.id.tv_task_status);
        }
    }

    ItemCLick itemCLick;

    public void setItemCLick(ItemCLick itemCLick) {
        this.itemCLick = itemCLick;
    }

    public interface ItemCLick {
        void item(PoiEntity poiEntity);
    }

    /**
     * 保存小数点后两位
     *
     * @param value
     * @return
     */
    public static String format2(double value) {
        DecimalFormat df = new DecimalFormat("0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df.format(value);

    }

    public static String format3(double value) {
        NumberFormat nf = NumberFormat.getNumberInstance();

        nf.setMaximumFractionDigits(2);

/** setMinimumFractionDigits设置成2
 * 如果不这么做，那么当value的值是100.00的时候返回100
 * 而不是100.00

 */

        nf.setMinimumFractionDigits(2);

        nf.setRoundingMode(RoundingMode.HALF_UP);

/*

如果想输出的格式用逗号隔开，可以设置成true

*/

        nf.setGroupingUsed(false);
        return nf.format(value);

    }

    public static String format5(double value) {
        return String.format("%.2f", value).toString();

    }
}
