import random
import time
import cv2
import numpy as np

from . import public

import sys
sys.path.append('../')
from etc import settings
from lib.functions import window_click, window_capture, get_match_points


def sanjie(hwnd, x, y, is_leader, skill_order=None):
    if is_leader:
        duihua_jixuzhuiji_img = cv2.cvtColor(cv2.imread("src/duihua_jixuzhuiji.png"), cv2.COLOR_BGR2GRAY)
        duihua_xiangshoumaiwo_img = cv2.cvtColor(cv2.imread("src/duihua_xiangshoumaiwo.png"), cv2.COLOR_BGR2GRAY)
        duihua_sanjie_end_img = cv2.cvtColor(cv2.imread("src/duihua_sanjie_end.png"), cv2.COLOR_BGR2GRAY)

        lower = np.array([20, 150, 140], dtype="uint8")
        upper = np.array([80, 240, 255], dtype="uint8")

        renwu_sanjiexuanshang_img_color = cv2.cvtColor(cv2.imread("src/renwu_sanjiexuanshang.png"), cv2.IMREAD_COLOR)
        mask = cv2.inRange(renwu_sanjiexuanshang_img_color, lower, upper)
        renwu_sanjiexuanshang_img_color = cv2.bitwise_and(renwu_sanjiexuanshang_img_color, renwu_sanjiexuanshang_img_color, mask=mask)
        renwu_sanjiexuanshang_img = cv2.cvtColor(renwu_sanjiexuanshang_img_color, cv2.COLOR_BGR2GRAY)

        #cv2.namedWindow("Image")
        #cv2.imshow("Image", renwu_huoban_img)
        #cv2.waitKey(0)
        #sys.exit(1)

        # 若锁屏，则解锁
        public.jiesuo(hwnd, x, y)

        # 切换挂机技能
        if skill_order is not None:
            public.change_skill(hwnd, x, y, skill_order)
        else:
            time.sleep(2.0)

        num_error = 0
        while True:
            if num_error >= 5:
                print("错误次数过多，退出")
                return

            time.sleep(random.uniform(settings.inverval_min, settings.inverval_max))

            src_img = window_capture(hwnd, x, y)

            # 对话
            points = get_match_points(src_img, duihua_jixuzhuiji_img)
            if points:
                px, py = points[0]
                pos = (px + 50, py + 20)
                randint_x = random.randint(0, 80)
                randint_y = random.randint(0, 5)
                window_click(hwnd, pos, randint_x, randint_y)
                continue

            # 对话
            points = get_match_points(src_img, duihua_xiangshoumaiwo_img, threshold=0.85)
            if points:
                px, py = points[0]
                pos = (px + 50, py + 20)
                randint_x = random.randint(0, 80)
                randint_y = random.randint(0, 5)
                window_click(hwnd, pos, randint_x, randint_y)
                continue

            # 对话
            points = get_match_points(src_img, duihua_sanjie_end_img)
            if points:
                pos = (2856 - 1927, 233 - 193)
                randint_x = random.randint(0, 10)
                randint_y = random.randint(0, 10)
                window_click(hwnd, pos, randint_x, randint_y)
                print("三界悬赏结束.")
                return

            # 是否在移动中?
            src_img = window_capture(hwnd, x, y)
            status = public.get_status(hwnd, x, y, src_img, npc_jiaohu=False, call_pet=True)

            if status != "standing":
                continue

            public.show_menu_renwu(hwnd, x, y)

            # 优先查找任务栏显示
            src_img = window_capture(hwnd, x, y, color=cv2.IMREAD_COLOR)
            # 注意点怪的时候，Y最多向上移25-30个像素，不然会点开人物界面
            left = 770
            top = 140
            w = 100
            h = 250
            opponent_img = src_img[top:top + h, left:left + w]
            mask = cv2.inRange(opponent_img, lower, upper)
            opponent_img = cv2.bitwise_and(opponent_img, opponent_img, mask=mask)
            opponent_img = cv2.cvtColor(opponent_img, cv2.COLOR_BGR2GRAY)

            points = get_match_points(opponent_img, renwu_sanjiexuanshang_img, threshold=0.7)
            if points:
                # 点击任务栏任务
                px, py = points[0]
                # px, py是相对于opponent_img的坐标，所以还要加上left和top
                pos = (px + 770 + 70, py + 140 + 15)
                randint_x = random.randint(0, 20)
                randint_y = random.randint(0, 10)
                window_click(hwnd, pos, randint_x, randint_y)
                continue
    else:
        button_close_img = cv2.cvtColor(cv2.imread("src/button_close.png"), cv2.COLOR_BGR2GRAY)

        # 若锁屏，则解锁
        public.jiesuo(hwnd, x, y)

        # 切换挂机技能
        if skill_order is not None:
            public.change_skill(hwnd, x, y, skill_order)

        wait_num = 0
        num = 0
        while True:
            time.sleep(random.uniform(settings.inverval_min, settings.inverval_max))
            num += 1

            src_img = window_capture(hwnd, x, y)

            points = get_match_points(src_img, button_close_img)
            if points:
                # 误点到战备，或者NPC对话，关闭
                time.sleep(random.uniform(3.8, 4.2))
                src_img = window_capture(hwnd, x, y)
                points = get_match_points(src_img, button_close_img)
                if points:
                    pos = points[0]
                    randint_x = random.randint(0, 5)
                    randint_y = random.randint(0, 5)
                    window_click(hwnd, pos, randint_x, randint_y)
                    continue

            # 是否在移动中?
            status = public.get_status(hwnd, x, y, src_img, npc_jiaohu=False, call_pet=True)
            if status == "standing":
                wait_num += 1
            else:
                wait_num = 0
            
            if wait_num >= 10:
                print("挂机时间过长，退出")
                return

            if num >= 300:
                # 点下任务栏，避免锁屏
                pos = (2863 - 1927, 303 - 193)
                randint_x = random.randint(0, 5)
                randint_y = random.randint(0, 5)
                window_click(hwnd, pos, randint_x, randint_y)
                num = 0