#
# KodeBlox Copyright 2019 Sayak Mukhopadhyay
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http: //www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import functools
import logging
import threading
import tkinter as tk
from os.path import dirname, join

import semantic_version
import sys
import time

import l10n
import myNotebook as nb
from config import config, appname, appversion
from py_discord_sdk import discordsdk as dsdk

plugin_name = "EliteRPC"

logger = logging.getLogger(f'{appname}.{plugin_name}')

_ = functools.partial(l10n.Translations.translate, context=__file__)

CLIENT_ID = 1013117837310693438
VERSION = '1.0'

# Add global var for Planet name (landing + around)
planet = '<Hidden>'
landingPad = '2'

this = sys.modules[__name__]  # For holding module globals

ship_data = {
    "sidewinder": "Sidewinder",
    "eagle": "Eagle",
    "hauler": "Hauler",
    "adder": "Adder",
    "adder_taxi": "Apex Shuttle",
    "empire_eagle": "Imperial Eagle",
    "viper": "Viper Mk III",
    "cobramkiii": "Cobra Mk III",
    "viper_mkiv": "Viper Mk IV",
    "diamondback": "Diamondback Scout",
    "cobramkiv": "Cobra Mk IV",
    "type6": "Type-6 Transporter",
    "dolphin": "Dolphin",
    "diamondbackxl": "Diamondback Explorer",
    "empire_courier": "Imperial Courier",
    "independant_trader": "Keelback",
    "asp_scout": "Asp Scout",
    "vulture": "Vulture",
    "asp": "Asp Explorer",
    "federation_dropship": "Federal Dropship",
    "type7": "Type-7 Transporter",
    "typex": "Alliance Chieftain",
    "federation_dropship_mkii": "Federal Assault Ship",
    "empire_trader": "Imperial Clipper",
    "typex_2": "Alliance Crusader",
    "typex_3": "Alliance Challenger",
    "federation_gunship": "Federal Gunship",
    "krait_light": "Krait Phantom",
    "krait_mkii": "Krait Mk II",
    "orca": "Orca",
    "ferdelance": "Fer-de-Lance",
    "mamba": "Mamba",
    "python": "Python",
    "type9": "Type-9 Heavy",
    "belugaliner": "Beluga Liner",
    "type9_military": "Type-10 Defender",
    "anaconda": "Anaconda",
    "federation_corvette": "Federal Corvette",
    "cutter": "Imperial Cutter"
}

def get_ship_name(ship_type):
    return ship_data.get(ship_type, "Unknown Ship")

def callback(result):
    logger.info(f'Callback: {result}')
    if result == dsdk.Result.ok:
        logger.info("Successfully set the activity!")
    elif result == dsdk.Result.transaction_aborted:
        logger.warning(f'Transaction aborted due to SDK shutting down: {result}')
    else:
        logger.error(f'Error in callback: {result}')
        raise Exception(result)


def update_presence():
    if isinstance(appversion, str):
        core_version = semantic_version.Version(appversion)

    elif callable(appversion):
        core_version = appversion()

    logger.info(f'Core EDMC version: {core_version}')
    if core_version < semantic_version.Version('5.0.0-beta1'):
        logger.info('EDMC core version is before 5.0.0-beta1')
        if config.getint("disable_presence") == 0:
            this.activity.state = this.presence_state
            this.activity.details = this.presence_details
            this.activity.assets.large_image = this.presence_large_image
            this.activity.assets.large_text = this.presence_large_text
            this.activity.assets.small_image = this.presence_small_image
            this.activity.assets.small_text = this.presence_small_text
    else:
        logger.info('EDMC core version is at least 5.0.0-beta1')
        if config.get_int("disable_presence") == 0:
            this.activity.state = this.presence_state
            this.activity.details = this.presence_details
            this.activity.assets.large_image = this.presence_large_image
            this.activity.assets.large_text = this.presence_large_text
            this.activity.assets.small_image = this.presence_small_image
            this.activity.assets.small_text = this.presence_small_text

    this.activity.timestamps.start = int(this.time_start)
    this.activity_manager.update_activity(this.activity, callback)


def plugin_prefs(parent, cmdr, is_beta):
    """
    Return a TK Frame for adding to the EDMC settings dialog.
    """
    if isinstance(appversion, str):
        core_version = semantic_version.Version(appversion)

    elif callable(appversion):
        core_version = appversion()

    logger.info(f'Core EDMC version: {core_version}')
    if core_version < semantic_version.Version('5.0.0-beta1'):
        logger.info('EDMC core version is before 5.0.0-beta1')
        this.disablePresence = tk.IntVar(value=config.get_int("disable_presence"))
    else:
        logger.info('EDMC core version is at least 5.0.0-beta1')
        this.disablePresence = tk.IntVar(value=config.get_int("disable_presence"))

    frame = nb.Frame(parent)
    nb.Checkbutton(frame, text="Disable Presence", variable=this.disablePresence).grid()
    nb.Label(frame, text='Version %s' % VERSION).grid(padx=10, pady=10, sticky=tk.W)

    return frame


def prefs_changed(cmdr, is_beta):
    """
    Save settings.
    """
    config.set('disable_presence', this.disablePresence.get())
    config.set('disable_presence', this.disableName.get())
    update_presence()


def plugin_start3(plugin_dir):
    this.plugin_dir = plugin_dir
    this.discord_thread = threading.Thread(target=check_run, args=(plugin_dir,))
    this.discord_thread.setDaemon(True)
    this.discord_thread.start()
    return 'EliteRPC'


def plugin_stop():
    this.activity_manager.clear_activity(callback)
    this.call_back_thread = None


def journal_entry(cmdr, is_beta, system, station, entry, state):
    global planet
    global landingPad
    presence_state = this.presence_state
    presence_details = this.presence_details
    presence_largeimage = this.presence_large_image
    presence_largetext = this.presence_large_text
    presence_smallimage = this.presence_small_image
    presence_smalltext = this.presence_small_text

    match entry['event']:
        case 'StartUp':
            presence_largetext = get_ship_name(('{ship}').format(ship=state['ShipType'])) + (' | {shipname}').format(shipname=state['ShipName']) + (' | {shipident}').format(shipident=state['ShipIdent'])
            presence_smalltext = ('CMDR {cmdr}').format(cmdr=cmdr)# + ('{power}').format(power=entry['event', 'Powerplay'])
            presence_largeimage = state['ShipType']
            presence_smallimage = 'edlogosm'
            presence_state = _('In system {system}').format(system=system)
            presence_details = _('Docked at {station}').format(station=station) if station else _('Flying in normal space')
        case 'Location':
            presence_largetext = get_ship_name(('{ship}').format(ship=state['ShipType'])) + (' | {shipname}').format(shipname=state['ShipName']) + (' | {shipident}').format(shipident=state['ShipIdent'])
            presence_smalltext = ('CMDR {cmdr}').format(cmdr=cmdr)#, power=state['Power'])
            presence_largeimage = state['ShipType']
            presence_smallimage = 'edlogosm'
            presence_state = _('In system {system}').format(system=system)
            presence_details = _('Docked at {station}').format(station=station) if station else _('Flying in normal space')
        case 'StartJump':
            presence_largetext = get_ship_name(('{ship}').format(ship=state['ShipType'])) + (' | {shipname}').format(shipname=state['ShipName']) + (' | {shipident}').format(shipident=state['ShipIdent'])
            presence_smalltext = ('CMDR {cmdr}').format(cmdr=cmdr)#, power=state['Power'])
            presence_largeimage = state['ShipType']
            presence_smallimage = 'edlogosm'
            presence_state = _('Jumping')
            presence_details = _('Jumping to system {system}').format(system=entry['StarSystem']) if entry['JumpType'] == 'Hyperspace' else _('Preparing for supercruise')
        case 'SupercruiseEntry':
            presence_state = _('In system {system}').format(system=system)
            presence_details = _('Supercruising')
        case 'SupercruiseExit':
            presence_state = _('In system {system}').format(system=system)
            presence_details = _('Flying in normal space')
        case 'FSDJump':
            presence_state = _('In system {system}').format(system=system)
            presence_details = _('Supercruising')
        case 'Docked':
            presence_state = _('In system {system}').format(system=system)
            presence_details = _('Docked at {station}').format(station=station)
        case 'Undocked':
            presence_state = _('In system {system}').format(system=system)
            presence_details = _('Flying near {station}').format(station=entry['StationName']) if entry['event'] in ('Undocked', 'DockingCancelled', 'DockingTimeout') else _('Flying in normal space')
        case 'ShutDown':
            presence_state = _('Main Menu')
            presence_details = ''
        case 'DockingGranted':
            landingPad = entry['LandingPad']
        case 'Music':
            if entry['MusicTrack'] == 'MainMenu':
                presence_state = _('In Main Menu')
                presence_details = ''
        case 'ApproachBody':
            presence_details = _('Approaching {body}').format(body=entry['Body'])
            planet = entry['Body']
        case 'Touchdown' if entry['PlayerControlled']:
            presence_details = _('Landed on {body}').format(body=planet)
        case 'Liftoff':
            presence_details = _('Flying around {body}').format(body=planet) if entry['PlayerControlled'] else _('In SRV on {body}, ship in orbit').format(body=planet)
        case 'LeaveBody':
            presence_details = _('Supercruising')
        case 'Disembark':
            presence_largetext = ('Using the {suitcurrent} suit.').format(suitcurrent=state['SuitCurrent'])
        case 'Embark':
            presence_largeimage = state['ShipType']
            presence_largetext = get_ship_name(('{ship}').format(ship=state['ShipType'])) + (' | {shipname}').format(shipname=state['ShipName']) + (' | {shipident}').format(shipident=state['ShipIdent'])
        case 'Loadout': # when switching ships
            presence_largeimage = state['ShipType']
            presence_largetext = get_ship_name(('{ship}').format(ship=state['ShipType'])) + (' | {shipname}').format(shipname=state['ShipName']) + (' | {shipident}').format(shipident=state['ShipIdent'])
        case 'LaunchSRV':
            presence_details = _('In SRV on {body}').format(body=planet)
        case 'DockSRV':
            presence_details = _('Landed on {body}').format(body=planet)
        case _:
            # No specific action for other events
            pass

    #if entry['TimePledged']:
    #    presence_smalltext = presence_smalltext + (' | {power}').format(power=state['Power'])

    if (presence_state != this.presence_state or
    presence_details != this.presence_details or
    presence_largeimage != this.presence_large_image or
    presence_largetext != this.presence_large_text or
    presence_smallimage != this.presence_small_image or
    presence_smalltext != this.presence_small_text) :
        this.presence_state = presence_state
        this.presence_details = presence_details
        this.presence_large_image = presence_largeimage
        this.presence_large_text = presence_largetext
        this.presence_small_image = presence_smallimage
        this.presence_small_text = presence_smalltext
        update_presence()


def check_run(plugin_dir):
    plugin_path = join(dirname(plugin_dir), plugin_name)
    retry = True
    while retry:
        time.sleep(1 / 10)
        try:
            this.app = dsdk.Discord(CLIENT_ID, dsdk.CreateFlags.no_require_discord, plugin_path)
            retry = False
        except Exception:
            pass

    this.activity_manager = this.app.get_activity_manager()
    this.activity = dsdk.Activity()

    this.call_back_thread = threading.Thread(target=run_callbacks)
    this.call_back_thread.setDaemon(True)
    this.call_back_thread.start()
    this.presence_state = _('Connecting CMDR Interface')
    this.presence_details = ''
    this.presence_large_image = 'edlogo'
    this.presence_large_text = 'EliteRPC'
    this.presence_small_image = ''
    this.presence_small_text = ''
    this.time_start = time.time()

    this.disablePresence = None
    this.disableName = None

    update_presence()


def run_callbacks():
    try:
        while True:
            time.sleep(1 / 10)
            this.app.run_callbacks()
    except Exception:
        check_run(this.plugin_dir)