import json
import netrc
from datetime import datetime
from platform import uname
from sys import platform
from typing import List, Optional, TypedDict

import openai
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

Music_Config = TypedDict(
    "Music_Config",
    {
        "ctx": Optional[str],
        "queue": List[str],
        "requester": List[str],
    },
)


# *=========================================== Predefines
MASTER_TIMEOUT = datetime.now()
INITIAL_TIME_WAIT = 500
MUZYKA: Music_Config = {"ctx": None, "queue": [], "requester": []}

LOGFILE = ""
NETRC_FILE = ""
MUSIC_FOLDER = ""
MEMORY_FIVE_SIARA = ""
MEMORY_FIVE_MUZYKA = ""
SETTINGS_FILE = ""
ENCODING = ""
GRAPHICS_PATH = ""
MUZYKA_MOJEGO_LUDU_HISTORIA = 1500
MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE = 15
MUZYKA_MOJEGO_LUDU_PLAJLISTA = 30

FILE_SERVICE_ADDRESS = "http://192.168.1.15:5000"
RADIO_HARBOR_ADDRESS = "http://192.168.1.15:54321"
SKIP_TRACK = "/skip"

GET_MP3 = "/mp3"
SEND_MP3 = "/update_mp3"
GET_PLAYLIST = "/get_music"
ADD_TO_PRIO_PLAYLIST = "/add_to_priority"
CREATE_PRIO_PLAYLIST = "/create_priority_playlist"

REQUEST_MUSIC = "/request_radio_file"
CLEAR_PRIO = "/clear_pr_pls"
LIBRARIAN_SERVICE_ADDRESS = "http://192.168.1.192:5001"
SEND_QUERY = "/query"
TIME_BETWEEN_CALLS = 100000
LAST_SPONTANEOUS_CALL = datetime.now()
HOST_ADDRESS = "192.168.1.191"
PORT_ADDRESS = 5000

# *=========================================== Platform Specific Predefines

if platform in ("linux", "linux2"):
    SEPARATOR_FILE_PATH = "/"
    if "microsoft-standard" in uname().release:
        LOGFILE = "/home/mtuszowski/conjurer/discord.log"
        MEMORY_FIVE_SIARA = "/home/mtuszowski/conjurer/pamiec.json"
        SYSTEM_GPT_SETTINGS = "/home/mtuszowski/conjurer/system_gpt_settings.json"
        MEMORY_FIVE_MUZYKA = "/home/mtuszowski/conjurer/pamiec_muzyki.json"
        MUSIC_FOLDER = "/mnt/g/Muzyka/"
        SETTINGS_FILE = "/home/mtuszowski/conjurer/settings.json"
        NETRC_FILE = "/home/mtuszowski/.netrc"
        LOGSTORE = "/home/mtuszowski/conjurer/logs/"
        ACCIDENT_LOG = "/home/mtuszowski/conjurer/accident_log.json"
        ENCODING = "utf-8"
        GRAPHICS_PATH = "/home/mtuszowski/conjurer/Conjurer_graphics/"
        DIR_PATH_SADOX = "/mnt/c/Users/mtusz/OneDrive/Dokumenty/Fansadox"

    else:
        LOGFILE = "/home/pi/Conjurer/discord.log"
        MEMORY_FIVE_SIARA = "/home/pi/Conjurer/pamiec.json"
        SYSTEM_GPT_SETTINGS = "/home/pi/Conjurer/system_gpt_settings.json"
        MEMORY_FIVE_MUZYKA = "/home/pi/Conjurer/pamiec_muzyki.json"
        MUSIC_FOLDER = "/home/pi/RetroPie/mp3/"
        SETTINGS_FILE = "/home/pi/Conjurer/settings.json"
        NETRC_FILE = "/home/pi/.netrc"
        LOGSTORE = "/home/pi/RetroPie/logs/"
        ACCIDENT_LOG = "/home/pi/Conjurer/accident_log.json"
        ENCODING = "utf-8"
        GRAPHICS_PATH = "/home/pi/RetroPie/Conjurer_graphics/"
        DIR_PATH_SADOX = "/home/pi/RetroPie/Fansadox/"


elif platform == "win32":
    LOGFILE = "discord.log"
    MEMORY_FIVE_SIARA = "pamiec.json"
    SYSTEM_GPT_SETTINGS = "system_gpt_settings.json"
    MEMORY_FIVE_MUZYKA = "pamiec_muzyki.json"
    MUSIC_FOLDER = "G:\\Muzyka\\"
    SETTINGS_FILE = "settings.json"
    NETRC_FILE = "C:\\Users\\mtusz\\.netrc"
    LOGSTORE = "C:\\Users\\mtusz\\OneDrive\\Pulpit\\Conjurer\\logs\\"
    ACCIDENT_LOG = "accident_log.json"
    ENCODING = "utf-8"
    DIR_PATH_SADOX = "C:\\Users\\mtusz\\OneDrive\\Dokumenty\\Fansadox\\"
    SEPARATOR_FILE_PATH = "\\"
with open(SETTINGS_FILE, "r", encoding=ENCODING) as f_settings_file:
    DATA = json.load(f_settings_file)
REMOTE_HOST_NAME = "openai"
netrc_mod = netrc.netrc(NETRC_FILE)
authTokens = netrc_mod.authenticators(REMOTE_HOST_NAME)
openai.api_key = authTokens[2]
OPENAICLIENT = openai.AsyncOpenAI(api_key=openai.api_key)
REMOTE_HOST_NAME = "discord"
authTokens = netrc_mod.authenticators(REMOTE_HOST_NAME)
TOKEN = authTokens[2]

REMOTE_HOST_NAME = "spotipy"
authTokens = netrc_mod.authenticators(REMOTE_HOST_NAME)
SPOTIFY_CTRL = spotipy.Spotify(
    client_credentials_manager=SpotifyClientCredentials(
        client_id=authTokens[0],
        client_secret=authTokens[2],
    )
)

WORD_REACTIONS = DATA["word_reactions"]
CYCLIC_WORDS = DATA["cyclic_words"]
for key in WORD_REACTIONS:
    WORD_REACTIONS[key][2] = datetime.now()
with open(MEMORY_FIVE_SIARA, "r+", encoding=ENCODING) as temp_memory_file:
    # First we load existing data into a dict.
    MESSAGE_TABLE = json.load(temp_memory_file)

with open(SYSTEM_GPT_SETTINGS, "r+", encoding=ENCODING) as temp_settings_file:
    # First we load existing data into a dict.
    GPT_SETTINGS = json.load(temp_settings_file)
with open(MEMORY_FIVE_MUZYKA, "r+", encoding=ENCODING) as temp_music_memory_file:
    # First we load existing data into a dict.
    MESSAGE_TABLE_MUZYKA = json.load(temp_music_memory_file)
SPECJALNE_ZIEMNIACZKI = GPT_SETTINGS[1]
ASSISTANTS = {}
