# vim: nosmartindent autoindent

import json
import math
import random
import subprocess
import sys
import time
from datetime import datetime, timedelta

import emoji
import ephem  # type: ignore
import pygame
import pygame.gfxdraw
import pytchat  # type: ignore
from playsound import playsound
from pygame.locals import *
from youtube import *

import play_movie
from aques import Aques
from common_const import *
from common_module import CommonModule
from mode import Mode
from talk import Talk


class Main:
    kita_x: float = CWindow.WIDTH / 2
    kita_y: float = 1000000.
    kita_arg: float = 0.

    jojoko_x: float = CWindow.WIDTH / 2
    jojoko_y: float = 1000000.
    jojoko_arg: float = 0.

    @classmethod
    def main (
            cls,
            argv: list,
            argc: int) \
    -> None:
        mode: Mode = Mode.NIZIKA
        match (argc > 1) and argv[1]:
            case '-g':
                mode = Mode.GOATOH

            case '-w':
                mode = Mode.DOUBLE

        nizika_mode: bool = mode == Mode.NIZIKA
        goatoh_mode: bool = mode == Mode.GOATOH
        double_mode: bool = mode == Mode.DOUBLE

        print (mode)

        # ウィンドゥの初期化
        pygame.init ()
        screen: pygame.Surface = pygame.display.set_mode (
                (CWindow.WIDTH, CWindow.HEIGHT))

        # 大月ヨヨコの観測値
        observer = ephem.Observer ()
        observer.lat, observer.lon = '35', '139'

        # き太く陽オブジェクト
        sun = ephem.Sun ()

        # 大月ヨヨコ・オブジェクト
        moon = ephem.Moon ()

        # 吹き出し
        balloon = pygame.transform.scale (pygame.image.load ('talking.png'),
                                          (CWindow.WIDTH, 384))
        if goatoh_mode:
            balloon = pygame.transform.flip (balloon, False, True)

        # 背景（昼）
        bg_day: pygame.Surface = pygame.transform.scale (
                pygame.image.load ('bg.jpg'),
                (CWindow.WIDTH, CWindow.HEIGHT))

        # 背景（夕方）
        bg_evening: pygame.Surface = pygame.transform.scale (
                pygame.image.load ('bg-evening.jpg'),
                (CWindow.WIDTH, CWindow.HEIGHT))

        # 背景（夜）
        bg_night: pygame.Surface = pygame.transform.scale (
                pygame.image.load ('bg-night.jpg'),
                (CWindow.WIDTH, CWindow.HEIGHT))

        # 背景の草
        bg_grass: pygame.Surface = pygame.transform.scale (
                pygame.image.load ('bg-grass.png'),
                (CWindow.WIDTH, CWindow.HEIGHT))

        # き太く陽
        kita: pygame.Surface = pygame.transform.scale (
                pygame.image.load ('sun.png'), (200, 200))

        # 大月ヨヨコ
        jojoko: pygame.Surface = pygame.transform.scale (
                pygame.image.load ('moon.png'), (200, 200))

        # 音声再生器の初期化
        pygame.mixer.init (frequency = 44100)

        # ニジカの “ぬ゛ぅ゛ぅ゛ぅ゛ん゛”
        noon = pygame.mixer.Sound ('noon.wav')

        # “あっ!”
        deerjika_oh = pygame.mixer.Sound ('oh.wav')

        # おやつタイムのテーマ
        snack_time_sound = pygame.mixer.Sound ('snack_time.wav')

        # ゴートうの “ﾑﾑﾑﾑ”
        mumumumu = pygame.mixer.Sound ('mumumumu.wav')

        # ゴートうの “ｸｻﾀﾍﾞﾃﾙ!!”
        kusa = pygame.mixer.Sound ('kusa.wav')

        # YouTube Chat オブジェクト
        live_chat = pytchat.create (video_id = YOUTUBE_ID)

        # デバッグ・メシジのフォント
        system_font = pygame.font.SysFont ('notosanscjkjp', 24, bold = True)

        # 視聴者コメントのフォント
        user_font = pygame.font.SysFont ('notosanscjkjp', 32, italic = True)

        # ニジカのフォント
        nizika_font = pygame.font.SysFont ('07nikumarufont', 50)

        # Youtube Chat から取得したコメントたち
        chat_items: list = []

        # 会話の履歴
        histories: list = []

        # おやつ記録
        has_snack = False

        while True:
            # 観測地の日づけ更新
            observer.date = datetime.now ().date ()

            # 日の出開始
            sunrise_start: datetime = (
                    (ephem.localtime (observer.previous_rising (sun))
                     - timedelta (minutes = 30)))

            # 日の出終了
            sunrise_end: datetime = sunrise_start + timedelta (hours = 1)

            # 日の入開始
            sunset_start: datetime = (
                    (ephem.localtime (observer.next_setting (sun))
                     - timedelta (minutes = 30)))

            # 日の入終了
            sunset_end: datetime = sunset_start + timedelta (hours = 1)

            # 時刻つき観測地
            observer_with_time: ephem.Observer = observer
            observer_with_time.date = datetime.now () - timedelta (hours = 9)

            # 日の角度
            sun.compute (observer_with_time)
            sun_alt: float = math.degrees (sun.alt)
            sun_az: float = math.degrees (sun.az)

            # 月の角度
            moon.compute (observer_with_time)
            moon_alt: float = math.degrees (moon.alt)
            moon_az: float = math.degrees (moon.az)

            # 月齢
            new_moon_dt: datetime = ephem.localtime (
                    ephem.previous_new_moon (observer_with_time.date))
            moon_days_old: float = (
                    (datetime.now () -  new_moon_dt).total_seconds ()
                    / 60 / 60 / 24)

            # 背景描画
            cls.draw_bg (screen, bg_day, bg_evening, bg_night, bg_grass,
                         kita, jojoko,
                         sunrise_start, sunrise_end, sunset_start, sunset_end,
                         sun_alt, sun_az, moon_alt, moon_az, moon_days_old)

            # 左上に時刻表示
            for i in range (4):
                screen.blit (
                        system_font.render (str (datetime.now ()), True, (0, 0, 0)),
                        (i % 2, i // 2 * 2))

            if live_chat.is_alive ():
                # Chat オブジェクトが有効

                # Chat 取得
                chat_items = live_chat.get ().items

                if chat_items:
                    # 溜まってゐる Chat からランダムに 1 つ抽出
                    chat_item = random.choice (chat_items)

                    # 投稿者情報を辞書化
                    chat_item.author = chat_item.author.__dict__

                    # 絵文字を復元
                    chat_item.message = emoji.emojize (chat_item.message)

                    message: str = chat_item.message

                    if nizika_mode:
                        goatoh_talking = False
                    if goatoh_mode:
                        goatoh_talking = True
                    if double_mode:
                        goatoh_talking = random.random () < .5

                    while True:
                        # ChatGPT API を呼出し，返答を取得
                        answer: str = Talk.main (message, chat_item.author['name'], histories, goatoh_talking).replace ('\n', ' ')

                        # 履歴に追加
                        histories = (histories
                                     + [{'role': 'user',      'content': message},
                                        {'role': 'assistant', 'content': answer}])[(-12):]

                        # ログ書込み
                        with open ('log.txt', 'a') as f:
                            f.write (f'{datetime.now ()}\t'
                                     + f'{json.dumps (chat_item.__dict__)}\t'
                                     + f'{answer}\n')

                        cls.draw_talking (screen, balloon, user_font, nizika_font,
                                          message, answer, mode,
                                          mode == 3 and goatoh_talking)

                        # 鳴く．
                        if goatoh_talking:
                            if random.random () < .1:
                                kusa.play ()
                            else:
                                mumumumu.play ()
                        else:
                            noon.play ()

                        time.sleep (1.5)

                        cls.read_out (answer, goatoh_talking)

                        if not double_mode or random.random () < .5:
                            break

                        cls.draw_bg (screen, bg_day, bg_evening, bg_night,
                                     bg_grass, kita, jojoko,
                                     sunrise_start, sunrise_end,
                                     sunset_start, sunset_end,
                                     sun_alt, sun_az, moon_alt, moon_az,
                                     moon_days_old)

                        chat_item.author = {'name':     'ゴートうひとり' if goatoh_talking else '伊地知ニジカ',
                                            'id':       '',
                                            'imageUrl': './favicon-goatoh.ico' if goatoh_talking else './favicon.ico'}
                        chat_item.message = histories.pop (-1)['content']

                        message = chat_item.message

                        goatoh_talking = not goatoh_talking
            else:
                # Chat オブジェクトが無効

                # 再生成
                live_chat = pytchat.create (video_id = YOUTUBE_ID)

            if has_snack and datetime.now ().hour == 14:
                has_snack = False

            pygame.display.update ()

            if (not has_snack) and datetime.now ().hour == 15:
                has_snack = True
                deerjika_oh.play ()
                time.sleep (0.6)
                snack_time_sound.play ()
                play_movie.main (screen, 'snack_time.mp4')
                query = 'おやつタイムだ!!!!'
                cls.draw_bg (screen, bg_day, bg_evening, bg_night, bg_grass,
                             kita, jojoko,
                             sunrise_start, sunrise_end, sunset_start, sunset_end,
                             sun_alt, sun_az, moon_alt, moon_az, moon_days_old)
                answer = Talk.main (query).replace ('\n', ' ')
                cls.draw_talking (screen, balloon, user_font, nizika_font,
                                  query, answer)
                noon.play ()
                time.sleep (1.5)
                cls.read_out (answer)

            for event in pygame.event.get ():
                if event.type == QUIT:
                    pygame.quit ()
                    sys.exit ()

    @staticmethod
    def read_out (
            answer: str,
            goatoh: bool    = False,
    ) -> None:
        # 返答の読上げを WAV ディタとして生成，取得
        wav: bytearray | None
        try:
            wav = Aques.main (answer, goatoh)
        except:
            wav = None

        # 読上げを再生
        if wav is not None:
            with open ('./nizika_talking.wav', 'wb') as f:
                f.write (wav)

            playsound ('./nizika_talking.wav')

        time.sleep (1)

    @staticmethod
    def draw_talking (
            screen:         pygame.Surface,
            balloon:        pygame.Surface,
            user_font:      pygame.font.Font,
            nizika_font:    pygame.font.Font,
            query:          str,
            answer:         str,
            mode:           Mode                = Mode.NIZIKA,
            flip:           bool                = False,
    ) -> None:
        # 吹出し描画（ニジカは上，ゴートうは下）
        nizika_mode = False
        goatoh_mode = False
        double_mode = False
        match mode:
            case Mode.NIZIKA:
                screen.blit (balloon, (0, 0))
                nizika_mode = True
            case Mode.GOATOH:
                screen.blit (balloon, (0, 384))
                goatoh_mode = True
            case Mode.DOUBLE:
                screen.blit (pygame.transform.flip (balloon, flip, False), (0, 0))
                double_mode = True

        # 視聴者コメント描画
        screen.blit (
                user_font.render (
                    ('> ' + (query
                             if (CommonModule.len_by_full (query) <= 21)
                             else (CommonModule.mid_by_full (query, 0, 19.5) + '...'))),
                    True, (0, 0, 0)),
                ((120, 70 + 384)
                 if goatoh_mode
                 else (120 + (64 if (double_mode and flip) else 0), 70)))

        # ニジカの返答描画
        screen.blit (
                nizika_font.render (
                    (answer
                     if CommonModule.len_by_full (answer) <= 16
                     else CommonModule.mid_by_full (answer, 0, 16)),
                    True,
                    (192, 0, 0)),
                (100, 150 + 384) if goatoh_mode else (100 + (64 if (double_mode and flip) else 0), 150))
        if CommonModule.len_by_full (answer) > 16:
            screen.blit (
                    nizika_font.render (
                        (CommonModule.mid_by_full (answer, 16, 16)
                         if CommonModule.len_by_full (answer) <= 32
                         else (CommonModule.mid_by_full (
                                   answer, 16, 14.5)
                               + '...')),
                         True,
                         (192, 0, 0)),
                    (100, 200 + 384) if goatoh_mode else (100 + (64 if (double_mode and flip) else 0), 200))

        pygame.display.update ()

    @classmethod
    def draw_bg (
            cls,
            screen:             pygame.Surface,
            bg_day:             pygame.Surface,
            bg_evening:         pygame.Surface,
            bg_night:           pygame.Surface,
            bg_grass:           pygame.Surface,
            kita_original:      pygame.Surface,
            jojoko_original:    pygame.Surface,
            sunrise_start:      datetime,
            sunrise_end:        datetime,
            sunset_start:       datetime,
            sunset_end:         datetime,
            sun_alt:            float,
            sun_az:             float,
            moon_alt:           float,
            moon_az:            float,
            moon_days_old:      float,
    ) -> None:
        sunrise_centre: datetime = (
                sunrise_start + (sunrise_end - sunrise_start) / 2)
        sunset_centre: datetime = (
                sunset_start + (sunset_end - sunset_start) / 2)

        jojoko: pygame.Surface = cls.get_jojoko (jojoko_original,
                                                 moon_days_old, moon_alt, moon_az)

        x = CWindow.WIDTH * (sun_az - 80) / 120
        y = ((CWindow.HEIGHT / 2)
             - (CWindow.HEIGHT / 2 + 100) * math.sin (math.radians (sun_alt)) / math.sin ( math.radians (60)))
        arg = math.degrees (math.atan2 (y - cls.kita_y, x - cls.kita_x))

        cls.kita_x = x
        cls.kita_y = y
        if abs (arg - cls.kita_arg) > 3:
            cls.kita_arg = arg

        kita: pygame.Surface = pygame.transform.rotate (kita_original, -(90 + cls.kita_arg))

        dt: datetime = datetime.now ()

        if sunrise_centre <= dt < sunset_centre:
            screen.blit (bg_day, (0, 0))
        else:
            screen.blit (bg_night, (0, 0))

        if sunrise_start <= dt < sunrise_end:
            bg_evening.set_alpha (255 - int ((abs (dt - sunrise_centre) * 510)
                                             / (sunrise_end - sunrise_centre)))
        elif sunset_start <= dt < sunset_end:
            bg_evening.set_alpha (255 - int ((abs (dt - sunset_centre) * 510)
                                             / (sunset_end - sunset_centre)))
        else:
            bg_evening.set_alpha (0)

        if sunrise_start <= dt < sunset_end:
            jojoko.set_alpha (255 - int (255 / 15 * abs (moon_days_old - 15)))
        else:
            jojoko.set_alpha (255)

        screen.blit (bg_evening, (0, 0))

        if (moon_az < 220) and (-10 <= moon_alt):
            screen.blit (jojoko, jojoko.get_rect (center = (cls.jojoko_x, cls.jojoko_y)))

            screen.blit (bg_grass, (0, 0))

        if (sun_az < 220) and (-10 <= sun_alt):
            screen.blit (kita, kita.get_rect (center = (cls.kita_x, cls.kita_y)))

            screen.blit (bg_grass, (0, 0))

    @classmethod
    def get_jojoko (
            cls,
            jojoko_original:    pygame.Surface,
            moon_days_old:      float,
            moon_alt:           float,
            moon_az:            float) \
    -> pygame.Surface:
        jojoko: pygame.Surface = jojoko_original.copy ()

        jojoko.set_colorkey ((0, 255, 0))

        for i in range (200):
            if 1 <= moon_days_old < 15:
                pygame.gfxdraw.bezier (jojoko, ((0, 100 + i), (100, 180 * moon_days_old / 7 - 80 + i), (200, 100 + i)), 3, (0, 255, 0))
            elif moon_days_old < 16:
                pass
            elif moon_days_old < 30:
                pygame.gfxdraw.bezier (jojoko, ((0, 100 - i), (100, 180 * (moon_days_old - 15) / 7 - 80 - i), (200, 100 - i)), 3, (0, 255, 0))
            else:
                jojoko.fill ((0, 255, 0))

        x = CWindow.WIDTH * (moon_az - 80) / 120
        y = ((CWindow.HEIGHT / 2)
             - (CWindow.HEIGHT / 2 + 100) * math.sin (math.radians (moon_alt)) / math.sin (math.radians (60)))
        arg = math.degrees (math.atan2 (y - cls.jojoko_y, x - cls.jojoko_x))

        cls.jojoko_x = x
        cls.jojoko_y = y
        if abs (arg - cls.jojoko_arg) > 3:
            cls.jojoko_arg = arg

        return pygame.transform.rotate (jojoko, -(90 + cls.jojoko_arg))


if __name__ == '__main__':
    Main.main (sys.argv, len (sys.argv))

