package com.wisewoods.eduandroid.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.SizeUtils;
import com.wisewoods.base.base.BaseVH;
import com.wisewoods.base.bean.VoteInfo;
import com.wisewoods.base.page.BasePageAdapter;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.databinding.ViewVoteOneBinding;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class VoteAdapter extends BasePageAdapter<VoteInfo.DataBean> {
    public VoteAdapter() {
        super(R.layout.item_reycler_vote);
    }

    private View getVoteView(VoteInfo.VoteListBean voteListBean, long voteTotalNum) {
        ViewVoteOneBinding viewVoteOneBinding = ViewVoteOneBinding.inflate(LayoutInflater.from(getContext()));
        viewVoteOneBinding.tvContent.setText(voteListBean.opt_desc);
        viewVoteOneBinding.tvNum.setText(voteListBean.opt_number + "");
        int v = (int) (voteListBean.opt_number * 100 / voteTotalNum);
        viewVoteOneBinding.tvPecent.setText("票  " + v + "%");
        viewVoteOneBinding.pb.setProgress(v);
        return viewVoteOneBinding.getRoot();
    }

    @Override
    protected void convert(@NotNull BaseVH baseVH, VoteInfo.DataBean dataBean) {
        baseVH.setText(R.id.tv_ticket_name, dataBean.voteTopic)
                .setText(R.id.tv_ticket_num, dataBean.voteDesc + "  " + dataBean.voteTotalNum + "人已投");

        List<VoteInfo.VoteListBean> voteList = dataBean.getVoteList();
        LinearLayout llTicket = baseVH.getView(R.id.ll_ticket);
        llTicket.removeAllViews();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.topMargin = SizeUtils.dp2px(5);
        for (VoteInfo.VoteListBean voteListBean : voteList) {
            llTicket.addView(getVoteView(voteListBean, dataBean.voteTotalNum), params);
        }
    }
}
