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

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import cn.hutool.core.thread.ThreadUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
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 org.opencv.core.Point;

import java.awt.AWTException;

/**
 * 探索-世界boss
 */
@SceneAnnotation("SceneMain_Explore")
public class SceneMain_Explore_World extends BaseScene implements Scene {
    @JsonIgnore
    @Override
    public float getPriority() {
        return 2.0f;
    }

    private static String PathEnter = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\enter.png";
    private static String PathCheck = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\check.png";
    private static String PathBack = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\back.png";
    private static String PathScan = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\scan.png";
    private static String PathScanDo = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\scanDo.png";
    private static String PathScanOver = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\scanOver.png";
    private static String PathStart = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\start.png";
    private static String PathStart2 = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\start2.png";
    private static String PathSkip = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\skip.png";
    private static String PathOver = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\over.png";
    private static String PathKilled = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\killed.png";
    private static String PathBoss = System.getProperty("user.dir") + "\\opencv\\scene\\explore\\world\\boss.png";

    @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(PathEnter, AiManage.getInstance().getCheckPoint())) {
                moveToClick(PathEnter, AiManage.getInstance().getCheckPoint());
                continue;
            }
        }
    }

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

    @Override
    public void think() throws AWTException {
        ThreadUtil.sleep(2000);
        if (checkHas(PathScan, AiManage.getInstance().getCheckPoint())) {
            moveToClick(PathScan, AiManage.getInstance().getCheckPoint());
            ThreadUtil.sleep(3000);
            for (int i = 0; i < 3; i++) {
                moveToClick(PathScanDo, AiManage.getInstance().getCheckPoint());
                ThreadUtil.sleep(8000);
            }
            moveToClick(PathScanOver, AiManage.getInstance().getCheckPoint());
            ThreadUtil.sleep(2000);
        }
        //往右走
        moveToClick(PathScan, AiManage.getInstance().getCheckPoint(),new Point(730,325));
        ThreadUtil.sleep(5000);
        moveToClick(PathScan, AiManage.getInstance().getCheckPoint(),new Point(730,325));
        ThreadUtil.sleep(5000);

        for (int i = 0; i < 3; i++) {
            if (checkHas(PathKilled, AiManage.getInstance().getCheckPoint())){
                break;
            }

            //打开界面
            moveToClick(PathBoss, AiManage.getInstance().getCheckPoint(),new Point(682,-219));
            ThreadUtil.sleep(2000);

            while (AiManage.getInstance().isRunning() && !checkHas(PathStart, AiManage.getInstance().getCheckPoint())){
                moveToClick(PathBoss, AiManage.getInstance().getCheckPoint(),new Point(694,-290));
                ThreadUtil.sleep(2000);
            }
            moveToClick(PathStart, AiManage.getInstance().getCheckPoint());
            ThreadUtil.sleep(3000);

            while (AiManage.getInstance().isRunning() && !checkHas(PathOver, AiManage.getInstance().getCheckPoint())){
                if (checkHas(PathSkip, AiManage.getInstance().getCheckPoint())){
                    moveToClick(PathSkip, AiManage.getInstance().getCheckPoint());
                    ThreadUtil.sleep(2000);
                }

                if (checkHas(PathStart2, AiManage.getInstance().getCheckPoint())){
                    moveToClick(PathStart2, AiManage.getInstance().getCheckPoint());
                    ThreadUtil.sleep(2000);
                }
                ThreadUtil.sleep(2000);
            }
            moveToClick(PathOver, AiManage.getInstance().getCheckPoint());
            ThreadUtil.sleep(3000);
        }
        getRecord().getData().setDone(true);

    }

    @Override
    public boolean isDone() {
        return getRecord().getData().isDone();
    }

    @Override
    public boolean isOpen() {
        // 获取当前日期和时间
        DateTime now = DateUtil.date();

        // 获取当前日期的星期
        Week week = DateUtil.dayOfWeekEnum(now);

        // 检查是否为周一、周三或周五
        boolean isSpecificWeekday = week == Week.MONDAY || week == Week.WEDNESDAY || week == Week.FRIDAY;

        // 检查当前时间是否在中午12点以后
        boolean isAfternoon = now.getField(DateField.HOUR_OF_DAY) >= 12;

        // 返回两个条件是否都满足
        return isSpecificWeekday && isAfternoon;
    }

}
