package com.bokecc.livemodule.view.gift;

import android.content.res.Configuration;
import android.view.ViewGroup;

import com.bokecc.gift.pojo.Gift;
import com.bokecc.livemodule.live.DWLiveCoreHandler;
import com.bokecc.livemodule.view.gift.interfaces.GiftCallBack;
import com.bokecc.livemodule.view.gift.interfaces.IGiftAnim;
import com.bokecc.livemodule.view.gift.interfaces.IGiftController;
import com.bokecc.sdk.mobile.live.common.log.ELog;

import java.util.ArrayList;
import java.util.LinkedList;

public abstract class BaseQueueHandler implements IGiftController {
    /**
     * 动画view的集合
     */
    protected ArrayList<IGiftAnim> list;
    private GiftCallBack giftCallBack;

    protected boolean isShowNum;
    protected ViewGroup viewGroup;
    protected int line;
    protected boolean isShowHead;
    protected float space;
    private int orientation = Configuration.ORIENTATION_PORTRAIT;
    /**
     * 核心队列数据结构。
     */
    private LinkedList<Gift> actionQueueList ;
    public BaseQueueHandler(GiftCallBack giftCallBack){
        this.giftCallBack=giftCallBack;
    }
    public void initView(ViewGroup viewGroup,int line,boolean isShowHead,boolean isShownum,float space,int showType){
        if ( line <=0){
            return;
        }
        this.viewGroup = viewGroup;
        this.line = line;
        this.isShowHead = isShowHead;
        this.isShowNum = isShownum;
        this.space = space;
        actionQueueList = new LinkedList<>();
        this.isShowNum = isShownum;
        list = new ArrayList<IGiftAnim>();
        initGiftView();
    }

    public abstract void initGiftView();

    public void addGift(Gift gift) {
        if (orientation == Configuration.ORIENTATION_LANDSCAPE){
            actionQueueList.clear();
            cancelAllGift();
            return;
        }
        //如果需要同一个人连续展示的话 判断是否有可追加的
        if (isShowNum){
            IGiftAnim joinableView = findJoinableView(gift);
            if (joinableView!=null){
                ELog.e("BaseQueueHandler","findJoinableView joinAnim "+joinableView.hashCode());
                joinableView.joinAnim(gift);
                return;
            }
        }
        //判断是否有空闲的
        IGiftAnim availableView = findAvailableView();
        if (availableView!=null){
            ELog.e("BaseQueueHandler","findAvailableView startAnim "+availableView.hashCode());
            availableView.startAnim(gift);
            return;
        }
        ELog.e("BaseQueueHandler","addGift 添加到队列 ");
        //如果上述两种都不符合 那么就需要放入队列中
        if (isCanAddHeader()){
            if (DWLiveCoreHandler.getInstance().getViewer().getId().equals(gift.getFromUserId())) {
                actionQueueList.addFirst(gift);
            } else {
                actionQueueList.add(gift);
            }
        } else {
            if (DWLiveCoreHandler.getInstance().getViewer().getId().equals(gift.getFromUserId())) {
                actionQueueList.addFirst(gift);
            } else {
                actionQueueList.add(gift);
            }
            actionQueueList.removeLast();
        }

    }

    private  boolean isCanAddHeader(){
        if (actionQueueList.size() < getMaxCount()){
            return true;
        }
        return false;
    }

    protected abstract int getMaxCount();

    public void cancelAllGift() {
        for (IGiftAnim giftAnim : list) {
            giftAnim.cancelAnim();
        }
    }

    /**
     * 查找可加入连击动画的Player。
     *
     * @return 如果成功找到，则返回该Player，否则返回null。
     */
    private IGiftAnim findJoinableView(Gift gift) {
        for (IGiftAnim giftAnim : list) {
            if (giftAnim.canJoin(gift)) {
                return giftAnim;
            }
        }
        return null;
    }

    /**
     * 查找可播放新动画的Player。
     *
     * @return 如果成功找到，则返回该Player，否则返回null。
     */
    private IGiftAnim findAvailableView() {
        for (IGiftAnim giftAnim : list) {
            if (giftAnim.available()) {
                return giftAnim;
            }
        }
        return null;
    }

    @Override
    public void onPlayerAvailable(IGiftAnim iGiftAnim) {
        if (actionQueueList.isEmpty()) {
            //通知外层隐藏空间
            ELog.e("BaseQueueHandler","onPlayerAvailable 动画加载完成");
            for (IGiftAnim giftAnim : list) {
                if (!giftAnim.available()) {
                    return ;
                }
            }
            if (giftCallBack!=null){
                giftCallBack.onFinish();
            }
        }else{
            if (actionQueueList.size() == 1){
                ELog.e("BaseQueueHandler","是一个了");
            }
            ELog.e("BaseQueueHandler","onPlayerAvailable 拿到了心的动画 "+actionQueueList.size()+"  iGiftAnim = "+iGiftAnim.hashCode());
            Gift poll = actionQueueList.pollFirst();
            iGiftAnim.startAnim(poll);
        }


    }

    public void setOnConfigurationChanged(Configuration newConfig){
        this.orientation = newConfig.orientation;
        actionQueueList.clear();
        cancelAllGift();
    }
}
