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

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bokecc.common.utils.Tools;
import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.utils.PopupAnimUtil;
import com.bokecc.livemodule.view.BasePopupWindow;
import com.bokecc.sdk.mobile.live.pojo.LotteryAction;
import com.bokecc.sdk.mobile.live.pojo.LotteryUserInfo;

import org.json.JSONObject;

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

/**
 * 抽奖开始弹出框
 */
public class OneArmHanditStartPopup extends BasePopupWindow {
    public static final int CHANGE_WHITE = 0x1;
    public static final int CHANGE_YELLOW = 0x1;

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == CHANGE_WHITE){
                iv_header.setBackgroundResource(R.mipmap.header_white_light);
                handler.sendEmptyMessageDelayed(CHANGE_YELLOW,500);
            } else {
                iv_header.setBackgroundResource(R.mipmap.one_arm_handit_header);
                handler.sendEmptyMessageDelayed(CHANGE_WHITE,500);
            }
        }
    };

    private LotteryAction lotteryAction;
    private OnClickShowReslutListener onClickShowReslutListener;
    public OneArmHanditStartPopup(Context context,OnClickShowReslutListener onClickShowReslutListener) {
        super(context);
        this.onClickShowReslutListener = onClickShowReslutListener;
    }

    ImageView closeBtn,iv_header;

    ImageView statue;
    TextView prize_name,gift_num,person_num;
    View rl_result;
    ArrayList<LotteryUserInfo> userInfos = new ArrayList<>();
    SlotMachineView slotMachineView;
    View layoutNone;
    private View rootView;

    private ArrayList<LotteryUserInfo> localUserInfo;


    @Override
    protected void onViewCreated() {
        closeBtn = findViewById(R.id.close);
        iv_header = findViewById(R.id.iv_header);
        statue = findViewById(R.id.statue);
        prize_name = findViewById(R.id.prize_name);
        gift_num = findViewById(R.id.gift_num);
        person_num = findViewById(R.id.person_num);
        rl_result = findViewById(R.id.rl_show_reslut);
        slotMachineView = findViewById(R.id.slotMachineView);
        slotMachineView.setOnClickMoreListener(new SlotMachineView.OnClickMoreListener() {
            @Override
            public void onClickMore() {
                if (onClickShowReslutListener != null){
                    onClickShowReslutListener.onClickShowReslut(rootView,lotteryAction);
                }
            }
        });
        layoutNone = findViewById(R.id.layoutNone);

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        localUserInfo = new ArrayList<>();
        initLocalData();
        rl_result.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickShowReslutListener != null){
                    onClickShowReslutListener.onClickShowReslut(rootView,lotteryAction);
                }
            }
        });
    }

    private void initLocalData(){
        LotteryUserInfo info1 = new LotteryUserInfo(new JSONObject());
        LotteryUserInfo info2 = new LotteryUserInfo(new JSONObject());
        LotteryUserInfo info3 = new LotteryUserInfo(new JSONObject());
        LotteryUserInfo info4 = new LotteryUserInfo(new JSONObject());
        LotteryUserInfo info5 = new LotteryUserInfo(new JSONObject());

        info1.setLocalavatar(R.mipmap.icon_local_like);
        info2.setLocalavatar(R.mipmap.icon_local_gift);
        info3.setLocalavatar(R.mipmap.icon_local_redpacket);
        info4.setLocalavatar(R.mipmap.icon_local_box);
        info5.setLocalavatar(R.mipmap.icon_local_more);

        localUserInfo.add(info1);
        localUserInfo.add(info2);
        localUserInfo.add(info3);
        localUserInfo.add(info4);
        localUserInfo.add(info5);
    }

    @Override
    protected int getContentView() {
        return R.layout.view_one_arm_handit_start;
    }

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

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

    public void setLotteryAction(View rootView, LotteryAction lotteryAction) {
        this.rootView = rootView;
        if (lotteryAction == null){
            Log.e("setLotteryAction","lotteryAction == null");
            return;
        }
        this.lotteryAction=lotteryAction;
        if (!TextUtils.isEmpty(lotteryAction.getPrizeName())){
            prize_name.setText(lotteryAction.getPrizeName());
        }

        if (!TextUtils.isEmpty(lotteryAction.getOnlineNumber())){
            person_num.setText(lotteryAction.getOnlineNumber());
        }

        if (!TextUtils.isEmpty(lotteryAction.getPrizeNum())){
            gift_num.setText(lotteryAction.getPrizeNum());
        }

        if (lotteryAction.getUserInfos() != null && lotteryAction.getUserInfos().size() != 0){
            userInfos.clear();
            if (lotteryAction.getUserInfos() != null && lotteryAction.getUserInfos().size() != 0) {
                userInfos.addAll(lotteryAction.getUserInfos());
            }
            userInfos.addAll(0,localUserInfo);
        }

        if (slotMachineView != null && userInfos != null &&userInfos.size() != 0){
            slotMachineView.setData(userInfos);
        }

        Tools.log("setLotteryAction","setLotteryAction ? userInfos == " + ((userInfos != null && userInfos.size() != 0) ? userInfos.size() : "null" ));

        start();
    }

    public void start(){
        handler.sendEmptyMessageDelayed(CHANGE_WHITE,500);
        rl_result.setVisibility(View.GONE);
        statue.setBackgroundResource(R.mipmap.lottring);
        slotMachineView.start();
    }

    public void stop(LotteryAction lotteryAction){
//        slotMachineView.setTouchable(false);
        this.lotteryAction = lotteryAction;
        List<LotteryUserInfo> list = lotteryAction.getLotteryWinInfo().getUserInfos();
        statue.setBackgroundResource(R.mipmap.icon_open_lottery);
        rl_result.setVisibility(View.VISIBLE);
        if (list != null && list.size() != 0) {
            layoutNone.setVisibility(View.GONE);
            slotMachineView.setVisibility(View.VISIBLE);
            slotMachineView.stop(list);
        } else {
            layoutNone.setVisibility(View.VISIBLE);
            slotMachineView.setVisibility(View.GONE);
        }
    }

    public void reset(){
        slotMachineView.reset();
    }

    public interface OnClickShowReslutListener{
        void onClickShowReslut(View rootView,LotteryAction lotteryAction);
    }

    @Override
    public void dismiss() {
        slotMachineView.reset();
        super.dismiss();
    }
}
