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.RoundView;

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


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


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

    public EarningDoughnutListAdapter(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_doughnut_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 {
        RoundView ivIcon;
        TextView tvTitle;
        TextView tvPercent;

        public ViewHolder(View itemView) {
            super(itemView);
            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)));
            }
            tvTitle.setText(bean.getName());
            tvPercent.setText("| " + Math.round(bean.getValue() / total * 100) + "%");
        }
    }
}
