package com.sencorsta.rabs.obj.ai.scene.glory;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.sencorsta.rabs.annotation.SceneAnnotation;
import com.sencorsta.rabs.obj.ai.AiManage;
import com.sencorsta.rabs.obj.ai.api.BaseScene;
import com.sencorsta.rabs.obj.ai.api.Scene;
import com.sencorsta.rabs.obj.ai.api.SceneWhitSubRecord;
import com.sencorsta.rabs.util.ResettableData;
import org.opencv.core.Point;

import java.awt.AWTException;

/**
 * 荣耀竞技-比赛
 */
@SceneAnnotation("SceneMain_Glory")
public class SceneMain_Glory_Match extends SceneWhitSubRecord implements Scene {
    @Override
    public float getPriority() {
        return 2.0f;
    }

    private static String PathCheck = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\check.png";
    private static String PathBack = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\back.png";
    private static String PathEnter = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\enter.png";
    private static String PathIknow = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\iknow.png";
    private static String PathStart = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\start.png";
    private static String PathStart2 = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\start2.png";
    private static String PathSkip = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\skip.png";
    private static String PathOver = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\over.png";
    private static String PathClose = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\close.png";
    private static String PathClose2 = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\close2.png";
    private static String PathReward = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\reward.png";
    private static String PathOb = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\ob.png";
    private static String PathLv = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\lv.png";
    private static String PathNotOpen = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\notOpen.png";
    private static String PathNotOpen2 = System.getProperty("user.dir") + "\\opencv\\scene\\glory\\match\\notOpen2.png";
    private int maxTime = 99;

    @Override
    public void back() throws AWTException {
        moveToClick(PathBack, AiManage.getInstance().getCheckPoint());
    }

    @Override
    public void open() throws AWTException {
        while (AiManage.getInstance().isRunning() && !check()) {
            AiManage.getInstance().updateWindow();
            ThreadUtil.sleep(2000);

            if (checkHas(PathIknow, AiManage.getInstance().getCheckPoint())) {
                moveToClick(PathIknow, AiManage.getInstance().getCheckPoint());
            }

            if (checkHas(PathEnter, AiManage.getInstance().getCheckPoint())) {
                moveToClick(PathEnter, AiManage.getInstance().getCheckPoint());
            }
        }
    }

    @Override
    public boolean check() throws AWTException {
        if (checkHas(PathCheck, AiManage.getInstance().getCheckPoint())) {
            return true;
        }
        if (checkHas(PathNotOpen2,AiManage.getInstance().getCheckPoint())){
            return true;
        }
        return false;
    }

    @Override
    public void think() throws AWTException {
        int count = getRecord().getData().getCount();

        ThreadUtil.sleep(2000);
        if (checkHas(PathNotOpen2,AiManage.getInstance().getCheckPoint())){
            System.out.println("活动未开启!");
            getRecordSub().getData().setDone(true);

            moveToClick(PathClose2, AiManage.getInstance().getCheckPoint());
            ThreadUtil.sleep(2000);
            return;
        }

        while (AiManage.getInstance().isRunning() && !isDone()) {
            ThreadUtil.sleep(3000);
            if (checkHas(PathNotOpen,AiManage.getInstance().getCheckPoint())){
                System.out.println("活动未开启!");
                getRecordSub().getData().setDone(true);
                break;
            }



            if (checkHas(PathStart, AiManage.getInstance().getCheckPoint())) {
                moveToClick(PathStart, AiManage.getInstance().getCheckPoint());
                ThreadUtil.sleep(5000);

                if (checkHas(PathStart2, AiManage.getInstance().getCheckPoint())) {
                    moveToClick(PathStart2, AiManage.getInstance().getCheckPoint());
                    ThreadUtil.sleep(2000);
                }else {
                    System.out.println("没有次数了!");
                    getRecordSub().getData().setDone(true);
                    break;
                }
            }
            while (AiManage.getInstance().isRunning() && !checkHas(PathOver, AiManage.getInstance().getCheckPoint())) {
//            if (checkHas(PathLose, AiManage.getInstance().getCheckPoint())) {
//                //发现失败 直接放弃
//                getRecord().getData().setCount(maxTime);
//                randomToClick();
//                ThreadUtil.sleep(2000);
//                return;
//            }
                if (checkHas(PathStart, AiManage.getInstance().getCheckPoint())) {
                    moveToClick(PathStart, AiManage.getInstance().getCheckPoint());
                    ThreadUtil.sleep(2000);
                }
                if (checkHas(PathStart2, AiManage.getInstance().getCheckPoint())) {
                    moveToClick(PathStart2, AiManage.getInstance().getCheckPoint());
                    ThreadUtil.sleep(2000);
                }
                if (checkHas(PathClose, AiManage.getInstance().getCheckPoint())) {
                    moveToClick(PathClose, AiManage.getInstance().getCheckPoint());
                    ThreadUtil.sleep(2000);
                }
                if (checkHas(PathSkip, AiManage.getInstance().getCheckPoint())) {
                    moveToClick(PathSkip, AiManage.getInstance().getCheckPoint());
                    ThreadUtil.sleep(2000);
                }
                ThreadUtil.sleep(2000);
            }
            randomToClick();
            getRecord().getData().setCount(count+1);
        }

        //领奖
        moveToClick(PathReward, AiManage.getInstance().getCheckPoint());
        ThreadUtil.sleep(2000);

        moveToClick(PathOb, AiManage.getInstance().getCheckPoint());
        ThreadUtil.sleep(3000);

        randomToClick();
        ThreadUtil.sleep(3000);


        moveToClick(PathLv, AiManage.getInstance().getCheckPoint());
        ThreadUtil.sleep(2000);

        moveToClick(PathReward, AiManage.getInstance().getCheckPoint());
        ThreadUtil.sleep(2000);

        moveToClick(PathOb, AiManage.getInstance().getCheckPoint());
        ThreadUtil.sleep(3000);

        randomToClick();
        ThreadUtil.sleep(3000);

        if (checkHas(PathClose, AiManage.getInstance().getCheckPoint())) {
            moveToClick(PathClose, AiManage.getInstance().getCheckPoint());
            ThreadUtil.sleep(2000);
        }

        moveToClick(PathClose2, AiManage.getInstance().getCheckPoint());
        ThreadUtil.sleep(2000);

    }


    @Override
    public boolean isDone() {
        return getRecord().getData().getCount() >= maxTime || getRecordSub().getData().isDone();
    }

    /**
     * 重置
     */
    @Override
    public ResettableData.ResetType getType() {
        return ResettableData.ResetType.MONTHLY;
    }


    /**
     * 是否开启
     */
    @Override
    public boolean isOpen() {
        // 获取当前时间
        DateTime now = DateUtil.date();
        // 获取当天的中午12点
        DateTime noon = DateUtil.beginOfDay(now).offset(DateField.HOUR_OF_DAY, 12);
        // 获取当天的24点（次日的0点）
        DateTime midnight = DateUtil.endOfDay(now);
        // 判断当前时间是否在中午12点到24点之间
        return now.isAfterOrEquals(noon) && now.isBeforeOrEquals(midnight);
    }


}
