package com.cooleshow.teacher.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 com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.EarningDoughnutChartBean;
import com.cooleshow.teacher.widgets.LineView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * Description:
 * Copyright  : Copyright (c) 2019
 * Company    : 大雅乐盟
 * Author     : r
 * Date       : 2019/9/18 19:49
 */
public class EarningStackedListAdapter extends BaseRecyclerAdapter<EarningStackedListAdapter.ViewHolder> {


    private Context mContext;
    private double total;
    List<EarningDoughnutChartBean.SeriesBean.DataBean> doughuntDataList = new ArrayList<>();
    private List<String> color = new ArrayList<>();

    public EarningStackedListAdapter(Context mContex) {
        this.mContext = mContex;
    }


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

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

    public void setData(List<EarningDoughnutChartBean.SeriesBean.DataBean> doughuntDataList, Double total, List<String> color) {
        this.total = total;
        this.doughuntDataList = doughuntDataList;
        this.color = color;
        notifyDataSetChanged();

    }


    public class ViewHolder extends BaseViewHolder {
        View line;
        LineView ivIcon;
        TextView tvTitle;
        TextView tvPercent;

        public ViewHolder(View itemView) {
            super(itemView);
            line=itemView.findViewById(R.id.line);
            ivIcon=itemView.findViewById(R.id.iv_icon);
            tvTitle=itemView.findViewById(R.id.tv_title);
            tvPercent=itemView.findViewById(R.id.tv_percent);
        }

        @Override
        protected void onBind(int position) {
            EarningDoughnutChartBean.SeriesBean.DataBean bean = doughuntDataList.get(position);
            if (position < color.size()) {
                ivIcon.setBgColor(Color.parseColor(color.get(position)));
            }
            if (position == 0) {
                line.setVisibility(View.GONE);
            } else {
                line.setVisibility(View.VISIBLE);
            }
            tvTitle.setText(bean.getName() + "");
            DecimalFormat df = new DecimalFormat("#,##0.00");
            tvPercent.setText(df.format(bean.getValue()));
        }
    }
}
