using System;
using Server;
using System.IO;
using Server.Misc;

namespace Server.Items
{
    public class LootController
    {
        //Data path to the config file
        private static string DirPath = Path.Combine(Path.Combine(Core.BaseDirectory, "Saves\\"), Path.Combine("LootController\\", "lootcontroller.bin"));
        
        //private variables for the gold control system
        private static bool     m_Enabled;
        private static double   m_TrammelGold;
        private static double   m_FeluccaGold;
        private static double   m_IlshenarGold;
        private static double   m_MalasGold;
        private static double   m_TokunoGold;
        private static double   m_TerMurGold;
       
        //private variables for the item intensity control system
        private static int m_TrammelMinIntensity;
        private static int m_TrammelMaxIntensity;
        private static int m_FeluccaMinIntensity;
        private static int m_FeluccaMaxIntensity;
        private static int m_IlshenarMinIntensity;
        private static int m_IlshenarMaxIntensity;
        private static int m_MalasMinIntensity;
        private static int m_MalasMaxIntensity;
        private static int m_TokunoMinIntensity;
        private static int m_TokunoMaxIntensity;
        private static int m_TerMurMinIntensity;
        private static int m_TerMurMaxIntensity;
        
        //public variables for the gold control system
        public static bool   Enabled         { get { return m_Enabled; }         set { m_Enabled = value; } }
        public static double TrammelGold     { get { return m_TrammelGold; }     set { m_TrammelGold = value; } }
        public static double FeluccaGold     { get { return m_FeluccaGold; }     set { m_FeluccaGold = value; } }
        public static double IlshenarGold    { get { return m_IlshenarGold; }    set { m_IlshenarGold = value; } }
        public static double MalasGold       { get { return m_MalasGold; }       set { m_MalasGold = value; } }
        public static double TokunoGold      { get { return m_TokunoGold; }      set { m_TokunoGold = value; } }
        public static double TerMurGold      { get { return m_TerMurGold; }      set { m_TerMurGold = value; } }
        
        //public variables for the item intensity control system
        public static int TrammelMinIntensity { get { return m_TrammelMinIntensity; } set { m_TrammelMinIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }
        public static int TrammelMaxIntensity { get { return m_TrammelMaxIntensity; } set { m_TrammelMaxIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }
        public static int FeluccaMinIntensity { get { return m_FeluccaMinIntensity; } set { m_FeluccaMinIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }
        public static int FeluccaMaxIntensity { get { return m_FeluccaMaxIntensity; } set { m_FeluccaMaxIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }
        public static int IlshenarMinIntensity { get { return m_IlshenarMinIntensity; } set { m_IlshenarMinIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }
        public static int IlshenarMaxIntensity { get { return m_IlshenarMaxIntensity; } set { m_IlshenarMaxIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }
        public static int MalasMinIntensity { get { return m_MalasMinIntensity; } set { m_MalasMinIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }
        public static int MalasMaxIntensity { get { return m_MalasMaxIntensity; } set { m_MalasMaxIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }
        public static int TokunoMinIntensity { get { return m_TokunoMinIntensity; } set { m_TokunoMinIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }
        public static int TokunoMaxIntensity { get { return m_TokunoMaxIntensity; } set { m_TokunoMaxIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }
        public static int TerMurMinIntensity { get { return m_TerMurMinIntensity; } set { m_TerMurMinIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }
        public static int TerMurMaxIntensity { get { return m_TerMurMaxIntensity; } set { m_TerMurMaxIntensity = value; if (value < 0) value = 0; if (value > 100)value = 100; } }

        // Initialize the variables for first time, then load config if it is in existance
        // otherwise create a new config file with the newly rendered values.
        public static void Initialize()
        {
            //version 1
            m_TrammelMinIntensity = 10;
            m_TrammelMaxIntensity = 80;
            m_FeluccaMinIntensity = 30;
            m_FeluccaMaxIntensity = 100;
            m_IlshenarMinIntensity = 20;
            m_IlshenarMaxIntensity = 100;
            m_MalasMinIntensity = 20;
            m_MalasMaxIntensity = 100;
            m_TokunoMinIntensity = 10;
            m_TokunoMaxIntensity = 80;
            m_TerMurMinIntensity = 10;
            m_TerMurMaxIntensity = 80;

            //version 0
            m_Enabled = true;
            m_TrammelGold = 0.875;
            m_FeluccaGold = 1.125;
            m_IlshenarGold = 1.125;
            m_MalasGold = 1.125;
            m_TokunoGold = 0.875;
            m_TerMurGold = 0.875;

            EventSink.WorldSave += new WorldSaveEventHandler(EventSink_WorldSave);
            LoadInformation();
        }

        /// <summary>
        /// This loads all the basic information from the config file, if a config file
        /// does not exsist, then create a new one.
        /// </summary>
        private static void LoadInformation()
        {
            string dir = Path.GetDirectoryName(DirPath);

            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            if (File.Exists(DirPath))
            {
                BinaryFileReader reader = new BinaryFileReader(new BinaryReader(File.OpenRead(DirPath)));
                int version = reader.ReadInt();

                switch (version)
                {
                    case 1:
                        {
                            m_TrammelMinIntensity = (int)reader.ReadInt();
                            m_TrammelMaxIntensity = (int)reader.ReadInt();
                            m_FeluccaMinIntensity = (int)reader.ReadInt();
                            m_FeluccaMaxIntensity = (int)reader.ReadInt();
                            m_IlshenarMinIntensity = (int)reader.ReadInt();
                            m_IlshenarMaxIntensity = (int)reader.ReadInt();
                            m_MalasMinIntensity = (int)reader.ReadInt();
                            m_MalasMaxIntensity = (int)reader.ReadInt();
                            m_TokunoMinIntensity = (int)reader.ReadInt();
                            m_TokunoMaxIntensity = (int)reader.ReadInt();
                            m_TerMurMinIntensity = (int)reader.ReadInt();
                            m_TerMurMaxIntensity = (int)reader.ReadInt();
                            goto case 0;
                        }
                    case 0:
                        {
                            m_Enabled       = (bool)reader.ReadBool();
                            m_TrammelGold   = (double)reader.ReadDouble();
                            m_FeluccaGold   = (double)reader.ReadDouble();
                            m_IlshenarGold  = (double)reader.ReadDouble();
                            m_MalasGold     = (double)reader.ReadDouble();
                            m_TokunoGold    = (double)reader.ReadDouble();
                            m_TerMurGold    = (double)reader.ReadDouble();
                            break;
                        }
                }

                if (version == 0)
                {
                    m_TrammelMinIntensity = 10;
                    m_TrammelMaxIntensity = 80;
                    m_FeluccaMinIntensity = 80;
                    m_FeluccaMaxIntensity = 100;
                    m_IlshenarMinIntensity = 20;
                    m_IlshenarMaxIntensity = 100;
                    m_MalasMinIntensity = 20;
                    m_MalasMaxIntensity = 100;
                    m_TokunoMinIntensity = 10;
                    m_TokunoMaxIntensity = 80;
                    m_TerMurMinIntensity = 10;
                    m_TerMurMaxIntensity = 80;
                }
                reader.Close();
            }
            else
            {
                BinaryFileWriter writer = new BinaryFileWriter(File.Create(DirPath), true);
                int version = 1;

                writer.Write((int)version);
                //version 1
                writer.Write((int)m_TrammelMinIntensity);
                writer.Write((int)m_TrammelMaxIntensity);
                writer.Write((int)m_FeluccaMinIntensity);
                writer.Write((int)m_FeluccaMaxIntensity);
                writer.Write((int)m_IlshenarMinIntensity);
                writer.Write((int)m_IlshenarMaxIntensity);
                writer.Write((int)m_MalasMinIntensity);
                writer.Write((int)m_MalasMaxIntensity);
                writer.Write((int)m_TokunoMinIntensity);
                writer.Write((int)m_TokunoMaxIntensity);
                writer.Write((int)m_TerMurMinIntensity);
                writer.Write((int)m_TerMurMaxIntensity);

                //Version 0
                writer.Write((bool)m_Enabled);
                writer.Write((double)m_TrammelGold);
                writer.Write((double)m_FeluccaGold);
                writer.Write((double)m_IlshenarGold);
                writer.Write((double)m_MalasGold);
                writer.Write((double)m_TokunoGold);
                writer.Write((double)m_TerMurGold);

                writer.Close();
            }
        }

        /// <summary>
        /// A hook to the worldsave event, save all the data.
        /// </summary>
        /// <param name="e">param is passed from the worldsave event</param>
        private static void EventSink_WorldSave(WorldSaveEventArgs e)
        {
            string dir = Path.GetDirectoryName(DirPath);

            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            BinaryFileWriter writer = null;

            if (File.Exists(DirPath))
                writer = new BinaryFileWriter(File.OpenWrite(DirPath), true);
            else
                writer = new BinaryFileWriter(File.Create(DirPath), true);

            int version = 1;

            writer.Write((int)version);

            //version 1
            writer.Write((int)m_TrammelMinIntensity);
            writer.Write((int)m_TrammelMaxIntensity);
            writer.Write((int)m_FeluccaMinIntensity);
            writer.Write((int)m_FeluccaMaxIntensity);
            writer.Write((int)m_IlshenarMinIntensity);
            writer.Write((int)m_IlshenarMaxIntensity);
            writer.Write((int)m_MalasMinIntensity);
            writer.Write((int)m_MalasMaxIntensity);
            writer.Write((int)m_TokunoMinIntensity);
            writer.Write((int)m_TokunoMaxIntensity);
            writer.Write((int)m_TerMurMinIntensity);
            writer.Write((int)m_TerMurMaxIntensity);

            //Version 0
            writer.Write((bool)m_Enabled);
            writer.Write((double)m_TrammelGold);
            writer.Write((double)m_FeluccaGold);
            writer.Write((double)m_IlshenarGold);
            writer.Write((double)m_MalasGold);
            writer.Write((double)m_TokunoGold);
            writer.Write((double)m_TerMurGold);

            writer.Close();
        }
    }
}