from io import BytesIO
from PIL import Image
from selenium.webdriver.common.actions.wheel_input import ScrollOrigin
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep

import constants
import cv2
import math
import numpy as np
import os
import utility

def generateURL(lat: float, lon: float, zoom: int):
    return f"https://www.google.com/maps/@{lon},{lat},{zoom}z"

class GoogleMap:
    initial_zoom: int
    initial_lat: float
    initial_lon: float
    pixel_ratio: float

    def __init__(self, initial_lat=49.103814, initial_lon=55.794258, initial_zoom=18):
        self.initial_lat = initial_lat
        self.initial_lon = initial_lon
        self.initial_zoom = initial_zoom
        self.pixel_ratio = constants.GOOGLE_PIXEL_RATIO[self.initial_zoom]

        options = webdriver.ChromeOptions()
        # options.add_experimental_option("detach", True)
        self.driver = webdriver.Chrome(options)
        self.driver.get(generateURL(initial_lat, initial_lon, initial_zoom))
        self.driver.maximize_window()

        action = ActionChains(self.driver)
        sleep(5)
        self.driver.execute_script('document.querySelector(\'.yHc72.qk5Wte\').click()')
        sleep(5)

    def open(self, lat, lon, zoom):
        self.initial_lat = lat
        self.initial_lon = lon
        self.initial_zoom = zoom
        self.pixel_ratio = constants.GOOGLE_PIXEL_RATIO[self.initial_zoom]
        self.driver.get(generateURL(lat, lon, zoom))

    def save_photo(self, filename: str):
        im = self.make_screenshot()
        im.save(filename)

    def destroy(self):
        self.driver.close()

    def get_size(self) -> tuple[int, int]:
        html = self.driver.find_element(By.TAG_NAME, 'html')
        return (html.size['width'], html.size['height'])

    def scroll(self, x: float, y: float, count: int = 1, inner_zoom: bool = True):
        html = self.driver.find_element(By.TAG_NAME, 'html')

        x_offset = (x - 0.5) * (html.size['width'] - 72) + 72
        y_offset = (y - 0.5) * html.size['height']
        action = ActionChains(self.driver)

        for i in range(count-1):
            action.scroll_from_origin(ScrollOrigin(html, int(x_offset), int(y_offset)), 0, -100 if inner_zoom else 100)
            action.perform()
            if i != count - 1:
                sleep(0.25)

    def move(self, dx: float, dy: float):
        self.driver.execute_script(utility.google_map_js_move_script(dx, dy))

    def make_as_center(self, x: float, y: float):
        dx = (x - 0.5) * self.get_size()[0]
        dy = (0.5 - y) * self.get_size()[1]
        self.move(dx, dy)
        sleep(1)

    def make_screenshot(self) -> Image.Image:
        png = self.driver.get_screenshot_as_png()
        im = Image.open(BytesIO(png))
        return utility.cv2_to_pil(np.array(im)[:, 72:])

    def get_geolocation(self):
        current_url = self.driver.current_url
        return utility.parse_google_maps_url(current_url)
