import numpy as np
import os
import time
from helper import (
    cv2,
    find_template,
    tap as tap_helper,
    swipe as swipe_helper,
    capture_current_screen,
    save_screenshot,
)

screenshot_address = os.getenv("ANDROID_ADDRESS")

# templates
close_sub_fights = cv2.imread("templates/close_sub_fights.jpg")
close_fight = cv2.imread("templates/close_fight.jpg")
fight_button = cv2.imread("templates/i.jpg")
end_of_log = cv2.imread("templates/end_of_log.jpg")
end_of_log2 = cv2.imread("templates/end_of_log2.jpg")

# cursor positions
fight_scroll_top = "2200 626"
fight_scroll_bottom = "2200 1500"
titan_fight = "1400 860"
damage_taken = "450 850"
close_details = "2175 450"
close_titan_fight = "2650 570"
defense_log = "350 870"


def non_max_suppression(boxes, overlapThresh):
    if len(boxes) == 0:
        return []

    # Convert to float
    boxes = np.array(boxes, dtype="float")

    # Initialize the list of picked indexes
    pick = []

    # Grab the coordinates of the bounding boxes
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]

    # Compute the area of the bounding boxes and sort by bottom-right y-coordinate
    area = (x2 - x1 + 1) * (y2 - y1 + 1)
    idxs = np.argsort(y2)

    # Keep looping while some indexes still remain in the indexes list
    while len(idxs) > 0:
        # Grab the last index in the indexes list and add the index value to the list of picked indexes
        last = len(idxs) - 1
        i = idxs[last]
        pick.append(i)

        # Find the largest (x, y) coordinates for the start of the bounding box and the smallest (x, y)
        # coordinates for the end of the bounding box
        xx1 = np.maximum(x1[i], x1[idxs[:last]])
        yy1 = np.maximum(y1[i], y1[idxs[:last]])
        xx2 = np.minimum(x2[i], x2[idxs[:last]])
        yy2 = np.minimum(y2[i], y2[idxs[:last]])

        # Compute the width and height of the bounding box
        w = np.maximum(0, xx2 - xx1 + 1)
        h = np.maximum(0, yy2 - yy1 + 1)

        # Compute the ratio of overlap
        overlap = (w * h) / area[idxs[:last]]

        # Delete all indexes from the index list that have overlap greater than the threshold
        idxs = np.delete(
            idxs, np.concatenate(([last], np.where(overlap > overlapThresh)[0]))
        )

    # Return only the bounding boxes that were picked
    return boxes[pick].astype("int")


def tap(location):
    tap_helper(location)
    time.sleep(1)


def swipe(start, end, duration=1000):
    swipe_helper(start, end, duration)
    time.sleep(0.5)


def tap_button(template):
    button = find_templates(template)
    if len(button) == 0:
        return
    tap(f"{button[0][0]} {button[0][1]}")


def is_end_of_log():
    templates = find_templates(end_of_log)
    if len(templates) == 0:
        templates = find_templates(end_of_log2)

    result = len(templates) > 0

    if result:
        print("reached end of guild war log!")

    return result


def find_max_y_pair(coordinates):
    # find the coordinate pair with the maximum y value
    result = max(coordinates, key=lambda x: x[1])

    return f"{result[0]} {result[1]}"


def snap():
    save_screenshot(screenshot_address)
    time.sleep(0.5)


def take_fight_screenshots():
    snap()

    # # if you desperately need submits
    # tap(damage_taken)
    # snap()

    tap_button(close_fight)
    time.sleep(1)


def find_templates(template):
    capture_current_screen()
    return find_template(template)


def process_war_log():
    buttons = find_templates(fight_button)

    if len(buttons) == 0:
        swipe(fight_scroll_bottom, fight_scroll_top)
        return

    # process all found buttons
    for pair in buttons:
        tap(f"{pair[0]} {pair[1]}")
        sub_buttons = find_templates(fight_button)
        if len(sub_buttons) == 0:
            take_fight_screenshots()
        else:
            for pair2 in sub_buttons:
                tap(f"{pair2[0]} {pair2[1]}")
                take_fight_screenshots()
            tap_button(close_sub_fights)

    swipe(find_max_y_pair(buttons), fight_scroll_top)


# start
while not is_end_of_log():
    process_war_log()

# possible duplicates here, but necessary to be sure to get the last fights
process_war_log()
