import os

from PIL import Image
from tqdm import tqdm

import module.config.server as server

server.server = 'cn'  # Don't need to edit, it's used to avoid error.

from module.map_detection.view import View
from module.base.base import ModuleBase
from module.config.config import AzurLaneConfig
from module.base.utils import *


class Config:
    """
    Paste the config of map file here
    """
    pass


"""
Usage:
    - Enter map, and find a siren.
    - Manually count the siren is in which node, in local map view.
    - Run relative_record.py first, to get enough images.
    - Run relative_record_gif2.py, to generate gif template file.
    - Find one suitable template in <FOLDER>/<NAME>_gif. It should:
        Not contain the face of siren, only contain the body.
        Not contain sea surface as background.
        Contain less frames if possible.
    - Copy to assets/<server>/template, run button_extract.py
    - Use new templates in config, like this:
        MAP_HAS_SIREN = True
        MAP_SIREN_TEMPLATE = ['U73', 'U81']

Arguments:
    CONFIG:     ini config file to load.
    FOLDER:     Folder to save.
    NAME:       Siren name, images will save in <FOLDER>/<NAME>
    NODE:       Node in local map view, that you are going to crop.
"""
CONFIG = 'alas'
FOLDER = ''
NAME = 'Deutschland'
NODE = 'D5'

if __name__ == '__main__':
    for folder in [FOLDER, os.path.join(FOLDER, NAME)]:
        if not os.path.exists(folder):
            os.mkdir(folder)

    cfg = AzurLaneConfig(CONFIG).merge(Config())
    al = ModuleBase(cfg)
    al.device.disable_stuck_detection()
    view = View(cfg)
    al.device.screenshot()
    view.load(al.device.image)
    grid = view[node2location(NODE.upper())]

    print('Please check if it is cropping the right area')
    print('If yes, wait until screenshot progress complete')
    print('If no, stop process, change `NODE`, run again')
    image = rgb2gray(grid.relative_crop((-0.5, -1, 0.5, 0), shape=(60, 60)))
    image = Image.fromarray(image, mode='L').show()

    images = []
    for n in tqdm(range(300)):
        images.append(al.device.screenshot())
    for n, image in enumerate(images):
        grid.image = np.array(image)
        image = rgb2gray(grid.relative_crop((-0.5, -1, 0.5, 0), shape=(60, 60)))
        image = Image.fromarray(image, mode='L')
        image.save(os.path.join(FOLDER, NAME, f'{n}.png'))

    print('relative_record done')
