package com.example.jmacro.wjdr.task;

import cn.hutool.json.JSONUtil;
import com.example.jmacro.wjdr.JMacro;
import com.example.jmacro.wjdr.base.ScreenPoint;
import com.example.jmacro.wjdr.base.ScreenRect;
import com.example.jmacro.wjdr.util.Logger;

import java.io.File;

public class TaskMining extends BaseTask {

    public TaskMining(JMacro jMacro, ScreenRect screenRect) {
        super(jMacro, screenRect);
    }

    @Override
    public void run() {
        Logger.info("》》》自动采矿开始》》》");

        Logger.info("判断是否在野外");
        ScreenRect 城镇 = jMacro.waitAndMatchPic(this.screenRect, "城镇_L449,887", 0.9);
        if (城镇 != null) {
            Logger.info("当前界面【野外】");
        } else {
            Logger.info("定位【野外】");
            ScreenRect 野外 = jMacro.waitAndMatchPic(this.screenRect, "野外_L449,885", 0.99);
            if (野外 == null) {
                Logger.error("定位【野外】失败，采矿终止");
                return;
            }
            Logger.error("定位【野外】成功，{}", 野外.toString());

            Logger.info("点击【野外】，坐标{}", JSONUtil.toJsonStr(野外.getCenter()));
            jMacro.mouseLeftClick(野外);
            Logger.info("定位野外按钮成功");
        }

        Logger.info("定位资源搜索按钮");
        ScreenRect 野外搜索 = jMacro.waitAndMatchPic(this.screenRect, "野外_搜索_L15,632", 0.8);
        if (野外搜索 == null) {
            Logger.error("未检测到【资源搜索按钮】，采矿终止");
            return;
        }
        Logger.info("定位资源搜索按钮成功：{}", 野外搜索.toString());
        jMacro.mouseLeftClick(野外搜索);

        String[] types = new String[]{
                "生肉", "木材", "煤矿", "铁矿"
        };
        for (String type : types) {
            Logger.info("搜索资源【{}】", type);
            boolean collect = collect(type, 0);
            if (collect) { // 中断采集
                break;
            }
        }

        // 检测是否是搜索界面
        ScreenRect 搜索 = jMacro.waitAndMatchPic(this.screenRect, "野外_搜索_L233,881", 0.98);
        if (搜索 != null) {
            Logger.info("退出资源搜索界面");
            jMacro.mouseLeftClick(this.screenRect);
        }

        ScreenRect 城1 = jMacro.waitAndMatchPic(this.screenRect, "城镇_L456,887", 0.9);
        if (城1 == null) {
            Logger.error("未检测到【城镇】，采矿终止");
            return;
        }
        Logger.info("返回城镇");
        Logger.info("》》》自动采矿结束，返回城镇》》》");
    }

    /**
     * @param type  资源类型
     * @param level 等级(0为重置)
     * @return 采集是否结束（满队则返回true,接下来的采集也不需要了）
     */
    private boolean collect(String type, int level) {
        Logger.info("定位【{}】图标", type);
        ScreenRect typeRect = jMacro.waitAndFindPic(screenRect, new File("legend", "野外_" + type + ".png"), 0.9);
        if (typeRect == null) {
            Logger.error("定位【{}】图标失败", type);
        }
        Logger.info("定位【{}】图标成功", type);

        Logger.info("单击【{}】图标，坐标[{},{}]", type, typeRect.getCenter()[0], typeRect.getCenter()[1]);
        jMacro.mouseLeftClick(typeRect);

        if (level == 0) {
            ScreenRect 等级减 = jMacro.waitAndFindPic(screenRect, new File("legend", "野外_资源_-.png"), 0.98);
            while (等级减 != null) {
                jMacro.delayTap();
                jMacro.mouseLeftClick(等级减);
                等级减 = jMacro.waitAndFindPic(screenRect, new File("legend", "野外_资源_-.png"), 0.98);
            }
            Logger.info("矿等级重置为1");
            level = 1;
        }

        Logger.info("搜索{}{}级矿", type, level);
        ScreenRect 搜索 = jMacro.waitAndFindPic(screenRect, new File("legend", "野外_资源_搜索.png"), 0.98);
        jMacro.mouseLeftClick(搜索);
        ScreenRect 采集 = jMacro.waitAndFindPic(screenRect, new File("legend", "野外_资源_采集.png"), 0.98);
        if (采集 == null) {
            Logger.info("未搜索到{}{}级矿！", type, level);
            ScreenRect 等级加 = jMacro.waitAndFindPic(screenRect, new File("legend", "野外_资源_+.png"), 0.98);
            jMacro.mouseLeftClick(等级加);
            return collect(type, level + 1);
        }
        Logger.info("搜索到{}{}级矿", type, level);
        Logger.info("采集{}{}级矿", type, level);
        jMacro.mouseLeftClick(采集);

        ScreenRect 采集队伍满 = jMacro.waitAndFindPic(screenRect, new File("legend", "野外_资源_采集队伍满.png"), 0.98);
        if (采集队伍满 != null) {
            Logger.info("出征队伍满，取消采集！");
            Logger.info("关闭弹框！");
            jMacro.mouseLeftClick(new ScreenPoint(采集队伍满.getRight() - 17, 采集队伍满.getBottom() - 20));
            Logger.info("退出资源搜索！");
            jMacro.mouseLeftClick(new ScreenPoint(采集队伍满.getRight() - 17, 采集队伍满.getBottom() - 20));
            return true;
        }

        ScreenRect 出征 = jMacro.waitAndFindPic(screenRect, new File("legend", "野外_资源_出征.png"), 0.98);
        if (出征 == null) {
            Logger.info("出征{}{}级矿失败！", type, level);
            return false;
        }
        Logger.info("出征{}{}级矿", type, level);
        jMacro.mouseLeftClick(出征);
        return true;
    }
}
