########################################################################################################################################################################
#       ::::    ::: :::::::::: ::::::::  ::::    ::: ::::::::::: ::::::::   ::::::::  :::        :::::::: 
#      :+:+:   :+: :+:       :+:    :+: :+:+:   :+:     :+:    :+:    :+: :+:    :+: :+:       :+:    :+: 
#     :+:+:+  +:+ +:+       +:+    +:+ :+:+:+  +:+     +:+    +:+    +:+ +:+    +:+ +:+       +:+         
#    +#+ +:+ +#+ +#++:++#  +#+    +:+ +#+ +:+ +#+     +#+    +#+    +:+ +#+    +:+ +#+       +#++:++#++   
#   +#+  +#+#+# +#+       +#+    +#+ +#+  +#+#+#     +#+    +#+    +#+ +#+    +#+ +#+              +#+    
#  #+#   #+#+# #+#       #+#    #+# #+#   #+#+#     #+#    #+#    #+# #+#    #+# #+#       #+#    #+#     
# ###    #### ########## ########  ###    ####     ###     ########   ########  ########## ########       
########################################################################################################################################################################
from multiprocessing.connection import wait
from multiprocessing.sharedctypes import Value
from optparse import Option
from random import choices
from secrets import randbelow
from time import sleep
from typing import Optional

import random
import time

import discord
from discord import app_commands

from typing import Literal, Union, NamedTuple
from enum import Enum

import os
from dotenv import load_dotenv

import json, requests
import sqlite3

####################################################################################
# Intents & Stuff.
####################################################################################

class MyClient(discord.Client):
    def __init__(self, *, intents: discord.Intents):
        super().__init__(intents=intents)
        self.tree = app_commands.CommandTree(self)
        self.synced = False

        #async def setup_hook(self):
            #await self.wait_until_ready()
            #if not self.synced:
                #await self.tree.sync()
                #self.synced = True 
            
    async def on_ready(self):
        await self.wait_until_ready()
        if not self.synced:
            await self.tree.sync()
            self.synced = True 
            print(f'Logged in as {client.user} (ID: {client.user.id})')
            print('------')

intents = discord.Intents.default()
client = MyClient(intents=intents)

####################################################################################
# Commands.
####################################################################################

@client.tree.command()
async def hello(interaction: discord.Interaction):
    """Says hello!"""
    await interaction.response.send_message(f'Hi, {interaction.user.mention} :wave:')

@client.tree.command()
async def opennotify(interaction: discord.Interaction, action: Literal['iss-now','astros']):
    """Uses Open-Notify To Get The Lastest Space Info."""
    if action == 'iss-now':
        url = requests.get("http://api.open-notify.org/iss-now.json")
        text = url.text
        data = json.loads(text)
        user = data

        info=discord.Embed(title='Current Position Of The ISS', description=user['iss_position'], color=0x000000)
        info.set_footer(text='http://api.open-notify.org/iss-now.json')
    elif action == 'astros':
        url = requests.get("http://api.open-notify.org/astros.json")
        text = url.text
        data = json.loads(text)
        user = data

        info=discord.Embed(title='Current Amount Of People In Space', description=user['number'], color=0x000000)
        info.set_footer(text='http://api.open-notify.org/astros.json')
    await interaction.response.send_message(embed=info)


@client.tree.command()
@app_commands.describe(
    http='The HTTP Input For Da Ketteh',
    embed='Allows You To Send The Message As A Embed.',
)
async def httpcat(interaction: discord.Interaction, http: int, embed: Optional[bool]):
    """Sends A Image From https://http.cat That Uses A Http Response! How Cool Is That?"""
    if embed == True:
        info=discord.Embed(title=f'https://http.cat/{http}', color=0x000000)
        info.set_image(url=f'https://http.cat/{http}')
        await interaction.response.send_message(embed=info)
    else:
        await interaction.response.send_message(f'https://http.cat/{http}')

@client.tree.command()
async def neontools(interaction: discord.Interaction):
    """Support Server And Bot Info."""
    button = discord.ui.Button(style=discord.ButtonStyle.url,label='Support Server',url='https://discord.gg/n85ChWrs8y')
    view = discord.ui.View()
    view.add_item(item=button)
    await interaction.response.send_message('NeonTools. Made By Neon.',view=view)

####################################################################################
# Fun
####################################################################################

@client.tree.command()
async def dice(interaction: discord.Interaction, first_num: int, second_num: int):
    """Rolls A Random Number Of Your Choosing (not really that random)"""
    result = random.randint(first_num, second_num)
    await interaction.response.send_message(f'You rolled {result}!')

@client.tree.command()
async def flip(interaction: discord.Interaction):
    """Flips A Coin If You Want To Settle A Debate."""
    result = random.choice(['heads', 'tails'])
    await interaction.response.send_message(f'The coin landed on {result}!')

@client.tree.command()
async def card(interaction: discord.Interaction):
    """Pulls Out A Random Card [doesnt work yet.]"""
    await interaction.response.send_message("This Doesnt Work Yet! :flower_playing_cards:")

@client.tree.command()
async def cookie(interaction: discord.Interaction):
    """Cookie Clicker But In Discord"""
    button = discord.ui.Button(style=discord.ButtonStyle.secondary,label='placeholder')
    view = discord.ui.View()
    view.add_item(item=button)
    await interaction.response.send_message('test',view=view)



####################################################################################
# Testing.
####################################################################################



####################################################################################
# Run
####################################################################################

load_dotenv()

token = os.getenv("DISCORD_TOKEN") # DISCORD_TOKEN_TEST, DISCORD_TOKEN

client.run(token)
