import asyncio
from typing import Any, cast

import imagetext_py
import PIL.Image

from konabot.common.path import ASSETS_PATH

from .base.fonts import HARMONYOS_SANS_SC_BLACK, HARMONYOS_SANS_SC_REGULAR, LXGWWENKAI_REGULAR

geimao_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "geimao.jpg").convert("RGBA")
pt_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "ptsay.png").convert("RGBA")
mnk_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "mnksay.jpg").convert("RGBA")
dasuan_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "dss.png").convert("RGBA")
suan_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "suanleba.png").convert("RGBA")
cute_ten_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "tententen.png").convert("RGBA")


def _draw_geimao(saying: str):
    img = geimao_image.copy()
    with imagetext_py.Writer(img) as iw:
        iw.draw_text_wrapped(
            saying, 960, 50, 0.5, 0, 1920, 240, HARMONYOS_SANS_SC_BLACK,
            imagetext_py.Paint.Color(imagetext_py.Color.from_hex("000000FF")),
            0.8,
            imagetext_py.TextAlign.Center,
            cast(Any, 30.0),
            imagetext_py.Paint.Color(imagetext_py.Color.from_hex("FFFFFFFF")),
            draw_emojis=True,
        )
    return img
    

async def draw_geimao(saying: str):
    return await asyncio.to_thread(_draw_geimao, saying)


def _draw_pt(saying: str):
    img = pt_image.copy()
    with imagetext_py.Writer(img) as iw:
        iw.draw_text_wrapped(
            saying, 259, 278, 0.5, 0.5, 360, 48, HARMONYOS_SANS_SC_REGULAR,
            imagetext_py.Paint.Color(imagetext_py.Color.from_hex("000000FF")),
            1.0,
            imagetext_py.TextAlign.Center,
            draw_emojis=True,
        )
    return img


async def draw_pt(saying: str):
    return await asyncio.to_thread(_draw_pt, saying)


def _draw_mnk(saying: str):
    img = mnk_image.copy()
    with imagetext_py.Writer(img) as iw:
        iw.draw_text_wrapped(
            saying, 540, 25, 0.5, 0, 1080, 120, HARMONYOS_SANS_SC_BLACK,
            imagetext_py.Paint.Color(imagetext_py.Color.from_hex("000000FF")),
            0.8,
            imagetext_py.TextAlign.Center,
            cast(Any, 15.0),
            imagetext_py.Paint.Color(imagetext_py.Color.from_hex("FFFFFFFF")),
            draw_emojis=True,
        )
    return img


async def draw_mnk(saying: str):
    return await asyncio.to_thread(_draw_mnk, saying)


def _draw_suan(saying: str, dasuan: bool = False):
    if dasuan:
        img = dasuan_image.copy()
    else:
        img = suan_image.copy()
    with imagetext_py.Writer(img) as iw:
        iw.draw_text_wrapped(
            saying, 1020, 290, 0.5, 0.5, 400, 48, LXGWWENKAI_REGULAR,
            imagetext_py.Paint.Color(imagetext_py.Color.from_hex("000000FF")),
            1.0,
            imagetext_py.TextAlign.Center,
            draw_emojis=True,
        )
    return img


async def draw_suan(saying: str, dasuan: bool = False):
    return await asyncio.to_thread(_draw_suan, saying, dasuan)


def _draw_cute_ten(saying: str):
    img = cute_ten_image.copy()
    with imagetext_py.Writer(img) as iw:
        iw.draw_text_wrapped(
            saying, 390, 479, 0.5, 0.5, 760, 96, LXGWWENKAI_REGULAR,
            imagetext_py.Paint.Color(imagetext_py.Color.from_hex("000000FF")),
            1.0,
            imagetext_py.TextAlign.Center,
            draw_emojis=True,
        )
    return img


async def draw_cute_ten(saying: str):
    return await asyncio.to_thread(_draw_cute_ten, saying)
