﻿using UnityEngine;
using ICities;

namespace CSkylinesMP
{
    public class Mod : IUserMod
    {

        // Returns mod's description, name or version.
        public string Description
        {
            get { return "Multiplayer mod created by PeterAS17 and Monkeymtv1. Contains code parts from Fr0sZ SkylinesMultiplayer mod. (Thank you!)"; }
        }
        public string Name
        {
            get { StartMod();  return Settings.ModSettings.Name; }
        }
        public string Version
        {
            get { return Settings.ModSettings.Version; }
        }
        public static bool LoadMultiplayerCode = false;

        /// <summary>
        /// Initializes the mod
        /// </summary>
        public void StartMod() {
            
            // TODO: Disable if not debug build.
            Settings.ModSettings.DeveloperDebug = true;

            DebugFile.WriteMessage("This mod is now running.", DebugFile.MessageType.Information);
            DebugFile.WriteMessage(Settings.ConnectionSettings.ConnectionPassword, DebugFile.MessageType.Information);
            if (GameObject.Find("CSkylinesMP_Base") == null)
            {
                new GameObject("CSkylinesMP_Base");
            }
            
        }

        /*
         * TODO
         * Is this useful...?
         * 
        public static bool IsEnabled() {
            var pluginManager = PluginManager.instance;
            var plugins = ReflectionHelper.GetPrivate<Dictionary<string, PluginManager.PluginInfo>>(pluginManager,
                "m_Plugins");

            foreach (var item in plugins)
            {
                if (item.Value.name != Settings.WorkshopID)
                {
                    continue;
                }

                return item.Value.isEnabled;
            }

            return false;
        }
        */

        public void OnSettingsUI(UIHelperBase helper)
        {
            UIHelperBase MainGroup = helper.AddGroup(Settings.ModSettings.Name);
            MainGroup.AddDropdown(Locale.GetPhrase(1), new string[] { Locale.GetPhrase(2), Locale.GetPhrase(3) }, 0, ChangeMPMode);

            //Not useful
            //MainGroup.AddSpace(0);
            UIHelperBase HostGroup = helper.AddGroup(Locale.GetPhrase(4));
            HostGroup.AddTextfield(Locale.GetPhrase(5), Settings.ConnectionSettings.ServerName, ChangeServerName, ChangeServerName);
            HostGroup.AddTextfield(Locale.GetPhrase(6), Settings.ConnectionSettings.Port.ToString(), ChangePort);
            HostGroup.AddTextfield(Locale.GetPhrase(7), Settings.ConnectionSettings.ConnectionPassword.ToString(), ChangeConnectionPassword);

            MainGroup.AddSpace(50);
            UIHelperBase ClientGroup = helper.AddGroup(Locale.GetPhrase(8));
            ClientGroup.AddTextfield(Locale.GetPhrase(9), Settings.ConnectionSettings.HostIP, ChangeHostIP, ChangeHostIP);
            ClientGroup.AddTextfield(Locale.GetPhrase(6), Settings.ConnectionSettings.Port.ToString(), ChangePort);
            ClientGroup.AddTextfield(Locale.GetPhrase(7), Settings.ConnectionSettings.ConnectionPassword.ToString(), ChangeConnectionPassword);

            MainGroup.AddSpace(20);
            UIHelperBase ButtonsGroup = helper.AddGroup("");
            ClientGroup.AddButton(Locale.GetPhrase(10), RunMultiplayerCode);
            ClientGroup.AddButton(Locale.GetPhrase(11), RunMultiplayerCode);
        }

#region Common Callbacks
        protected void ChangeMPMode(int Index)
        {
            switch (Index)
            {
                case 0:
                    Settings.ConnectionSettings.IsHosting = false;
                    break;
                case 1:
                    Settings.ConnectionSettings.IsHosting = true;
                    break;
            }
        }
        protected void ChangeHostIP(string IP)
        {
            /* TODO:
             * Check if the IP is valid.
            */
            Settings.ConnectionSettings.HostIP = IP;
        }
        protected void ChangePort(string Port)
        {
            if(ushort.Parse(Port) <= ushort.MaxValue) Settings.ConnectionSettings.Port = ushort.Parse(Port);
        }
        protected void ChangeServerName(string SrvName)
        {
            if (string.IsNullOrWhiteSpace(SrvName)) {
                Settings.ConnectionSettings.ServerName = "No name";
            } else {
                Settings.ConnectionSettings.ServerName = SrvName;
            }
        }
        protected void ChangeConnectionPassword(string Password)
        {
            Settings.ConnectionSettings.ConnectionPassword = Password;
        }
        protected void RunMultiplayerCode() { LoadMultiplayerCode = true; }
        public static void Null() { }
        public static bool ReturnFalse() { return false; }
        public static int SetFinalSpeed() { return 1; }
        #endregion

    }

}
