package com.bokecc.livemodule.live.function.redpacket.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.live.function.redpacket.adapter.RedPacketRankAdapter;
import com.bokecc.livemodule.utils.PopupAnimUtil;
import com.bokecc.livemodule.view.BasePopupWindow;
import com.bokecc.sdk.mobile.live.DWLive;
import com.bokecc.sdk.mobile.live.pojo.RedPacketRankInfo;

/**
 * 随堂测统计弹出界面
 */
public class RedPacketRankPopup extends BasePopupWindow {

    private ViewGroup creditRoot;       //学分的跟布局
    private TextView creditNum;         //学分
    private TextView creditText;        //学分提示
    private ViewGroup titleRoot;        //姓名和分数的标题
    private RecyclerView rankList;
    private ImageView ivRankClose;
    private RedPacketRankAdapter redPacketRankAdapter;
    private LinearLayout noCreditRoot;
    private ScrollView noRankRoot;


    // 构造函数
    public RedPacketRankPopup(Context context) {
        super(context);
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void onViewCreated() {
        ivRankClose = findViewById(R.id.iv_rank_close);
        ivRankClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //有数据的view
        creditRoot = findViewById(R.id.credit_root);
        creditNum = findViewById(R.id.credit_num);
        creditText = findViewById(R.id.credit_text);
        titleRoot = findViewById(R.id.title_root);
        rankList = findViewById(R.id.rank_list);
        //无数据的view
        noCreditRoot = findViewById(R.id.no_credit_root);

        noRankRoot = findViewById(R.id.no_red_packet_rank_root);

        redPacketRankAdapter = new RedPacketRankAdapter(mContext);
        rankList.setLayoutManager(new LinearLayoutManager(mContext));
        rankList.setAdapter(redPacketRankAdapter);
    }
    public void showSelfView(boolean isShowNo){
        if (isShowNo){
            creditRoot.setVisibility(View.GONE);
            creditText.setVisibility(View.GONE);
            noCreditRoot.setVisibility(View.VISIBLE);
        }else{
            creditRoot.setVisibility(View.VISIBLE);
            creditText.setVisibility(View.VISIBLE);
            noCreditRoot.setVisibility(View.GONE);
        }
    }
    public void showListView(boolean isShowNo){
        if (isShowNo){
            titleRoot.setVisibility(View.GONE);
            rankList.setVisibility(View.GONE);
            noRankRoot.setVisibility(View.VISIBLE);
        }else{
            titleRoot.setVisibility(View.VISIBLE);
            rankList.setVisibility(View.VISIBLE);
            noRankRoot.setVisibility(View.GONE);
        }
    }
    public void showDate(RedPacketRankInfo redPacketRankInfo){
        if (redPacketRankInfo ==null ||redPacketRankInfo.getRankList()==null){
            showSelfView(true);
            showListView(true);
            return;
        }
        int selfAmount = 0;
        String selfId = "";
        if (DWLive.getInstance().getViewer()!=null){
            selfId = DWLive.getInstance().getViewer().getId();
        }
        for (RedPacketRankInfo.RedPacketRankBean redPacketRankBean: redPacketRankInfo.getRankList()){
            if (redPacketRankBean.getUserId().equals(selfId)){
                selfAmount = redPacketRankBean.getAmount();
            }
        }
        if (selfAmount>0){
            showSelfView(false);
            creditNum.setText(String.valueOf(selfAmount));
        }else{
            showSelfView(true);
        }
        if (redPacketRankInfo.getRankList().size()>0){
            showListView(false);
            redPacketRankAdapter.add(redPacketRankInfo.getRankList());
        }else{
            showListView(true);
        }
    }
    @Override
    protected int getContentView() {
        return R.layout.red_packet_rank;
    }

    @Override
    protected Animation getEnterAnimation() {
        return PopupAnimUtil.getDefScaleEnterAnim();
    }

    @Override
    protected Animation getExitAnimation() {
        return PopupAnimUtil.getDefScaleExitAnim();
    }


    //---------------------------解决重复弹出的问题---------------------------------
    private boolean isShow = false;

    @Override
    public boolean isShowing() {
        if (isShow) {
            return true;
        }
        return super.isShowing();
    }

    @Override
    public void show(View view) {
        super.show(view);
        isShow = true;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        isShow = false;
    }
}