/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSim Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.IO;
using System.Reflection;
using libsecondlife;
using log4net;

namespace OpenSim.Framework
{
    public class EstateSettings
    {
        private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private ConfigurationMember configMember;

        //Settings to this island
        private float m_billableFactor;

        private uint m_estateID;
        private LLUUID m_estateManager0;
        private LLUUID m_estateManager1;
        private LLUUID m_estateManager2;
        private LLUUID m_estateManager3;
        private LLUUID m_estateManager4;
        private LLUUID m_estateManager5;
        private LLUUID m_estateManager6;
        private LLUUID m_estateManager7;
        private LLUUID m_estateManager8;
        private LLUUID m_estateManager9;
        private string m_estateName;

        private uint m_parentEstateID;
        private int m_pricePerMeter;
        private int m_redirectGridX;
        private int m_redirectGridY;

        public EstateSettings()
        {
            // Temporary hack to prevent multiple loadings.
            if (configMember == null)
            {
                configMember =
                    new ConfigurationMember(Path.Combine(Util.configDir(), "estate_settings.xml"), "ESTATE SETTINGS",
                                            loadConfigurationOptions, handleIncomingConfiguration, true);
                configMember.performConfigurationRetrieve();
            }
        }

        public float billableFactor
        {
            get { return m_billableFactor; }
            set
            {
                m_billableFactor = value;
                configMember.forceSetConfigurationOption("billable_factor", m_billableFactor.ToString());
            }
        }

        public uint estateID
        {
            get { return m_estateID; }
            set
            {
                m_estateID = value;
                configMember.forceSetConfigurationOption("estate_id", m_estateID.ToString());
            }
        }

        public uint parentEstateID
        {
            get { return m_parentEstateID; }
            set
            {
                m_parentEstateID = value;
                configMember.forceSetConfigurationOption("parent_estate_id", m_parentEstateID.ToString());
            }
        }

        public int redirectGridX
        {
            get { return m_redirectGridX; }
            set
            {
                m_redirectGridX = value;
                configMember.forceSetConfigurationOption("redirect_grid_x", m_redirectGridX.ToString());
            }
        }

        public int redirectGridY
        {
            get { return m_redirectGridY; }
            set
            {
                m_redirectGridY = value;
                configMember.forceSetConfigurationOption("redirect_grid_y", m_redirectGridY.ToString());
            }
        }

        public int pricePerMeter
        {
            get { return m_pricePerMeter; }
            set
            {
                m_pricePerMeter = value;
                configMember.forceSetConfigurationOption("price_per_meter", m_pricePerMeter.ToString());
            }
        }

        // Estate name

        public string estateName
        {
            get { return m_estateName; }
            set
            {
                m_estateName = value;
                configMember.forceSetConfigurationOption("estate_name", m_estateName.ToString());
            }
        }

        public LLUUID[] estateManagers
        {
            get
            {
                // returns a condensed array of LLUUIDs
                return GetEstateManagers();
            }
            set
            {
                // Sets a Condensed array of LLUUIDS
                int i = 0;
                for (i = 0; i < value.Length; i++)
                {
                    switch (i)
                    {
                        case 0:
                            m_estateManager0 = value[i];
                            break;
                        case 1:
                            m_estateManager1 = value[i];
                            break;
                        case 2:
                            m_estateManager2 = value[i];
                            break;
                        case 3:
                            m_estateManager3 = value[i];
                            break;
                        case 4:
                            m_estateManager4 = value[i];
                            break;
                        case 5:
                            m_estateManager5 = value[i];
                            break;
                        case 6:
                            m_estateManager6 = value[i];
                            break;
                        case 7:
                            m_estateManager7 = value[i];
                            break;
                        case 8:
                            m_estateManager8 = value[i];
                            break;
                        case 9:
                            m_estateManager9 = value[i];
                            break;
                    }
                }

                // Clear the rest of them..   as they're no longer valid
                for (int j = i; j < 10; j++)
                {
                    switch (j)
                    {
                        case 0:
                            m_estateManager0 = LLUUID.Zero;
                            break;
                        case 1:
                            m_estateManager1 = LLUUID.Zero;
                            break;
                        case 2:
                            m_estateManager2 = LLUUID.Zero;
                            break;
                        case 3:
                            m_estateManager3 = LLUUID.Zero;
                            break;
                        case 4:
                            m_estateManager4 = LLUUID.Zero;
                            break;
                        case 5:
                            m_estateManager5 = LLUUID.Zero;
                            break;
                        case 6:
                            m_estateManager6 = LLUUID.Zero;
                            break;
                        case 7:
                            m_estateManager7 = LLUUID.Zero;
                            break;
                        case 8:
                            m_estateManager8 = LLUUID.Zero;
                            break;
                        case 9:
                            m_estateManager9 = LLUUID.Zero;
                            break;
                    }
                }

                for (i = 0; i < 10; i++)
                {
                    // Writes out the Estate managers to the XML file.
                    configMember.forceSetConfigurationOption("estate_manager_" + i, (GetEstateManagerAtPos(i)).ToString());
                }
            }
        }

        #region EstateManager Get Methods to sort out skipped spots in the XML (suser error)

        private LLUUID GetEstateManagerAtPos(int pos)
        {
            // This is a helper for writing them out to the xml file
            switch (pos)
            {
                case 0:
                    return m_estateManager0;

                case 1:
                    return m_estateManager1;

                case 2:
                    return m_estateManager2;

                case 3:
                    return m_estateManager3;

                case 4:
                    return m_estateManager4;

                case 5:
                    return m_estateManager5;

                case 6:
                    return m_estateManager6;

                case 7:
                    return m_estateManager7;

                case 8:
                    return m_estateManager8;

                case 9:
                    return m_estateManager9;

                default:
                    return LLUUID.Zero;
            }
        }

        private LLUUID[] GetEstateManagers()
        {
            int numEstateManagers = GetNumberOfEstateManagers();
            LLUUID[] rEstateManagers = new LLUUID[numEstateManagers];

            int pos = 0;

            for (int i = 0; i < numEstateManagers; i++)
            {
                pos = GetNextEstateManager(pos);

                rEstateManagers[i] = GetEstateManagerAtPos(pos);
                pos++;
            }
            return rEstateManagers;
        }

        private int GetNextEstateManager(int startpos)
        {
            // This is a utility function that skips over estate managers set to LLUUID.Zero
            int i = startpos;
            for (i = startpos; i < 10; i++)
            {
                if (GetEstateManagerAtPos(i) != LLUUID.Zero) return i;
            }
            return i;
        }

        private int GetNumberOfEstateManagers()
        {
            // This function returns the number of estate managers set
            // Regardless of whether there is a skipped spot
            int numEstateManagers = 0;
            if (m_estateManager0 != LLUUID.Zero) numEstateManagers++;
            if (m_estateManager1 != LLUUID.Zero) numEstateManagers++;
            if (m_estateManager2 != LLUUID.Zero) numEstateManagers++;
            if (m_estateManager3 != LLUUID.Zero) numEstateManagers++;
            if (m_estateManager4 != LLUUID.Zero) numEstateManagers++;
            if (m_estateManager5 != LLUUID.Zero) numEstateManagers++;
            if (m_estateManager6 != LLUUID.Zero) numEstateManagers++;
            if (m_estateManager7 != LLUUID.Zero) numEstateManagers++;
            if (m_estateManager8 != LLUUID.Zero) numEstateManagers++;
            if (m_estateManager9 != LLUUID.Zero) numEstateManagers++;

            return numEstateManagers;
        }

        public void AddEstateManager(LLUUID avatarID)
        {
            LLUUID[] testateManagers = GetEstateManagers();
            LLUUID[] nestateManagers = new LLUUID[testateManagers.Length + 1];

            int i = 0;
            for (i = 0; i < testateManagers.Length; i++)
            {
                nestateManagers[i] = testateManagers[i];
            }

            nestateManagers[i] = avatarID;

            //Saves it to the estate settings file
            estateManagers = nestateManagers;
        }

        public void RemoveEstateManager(LLUUID avatarID)
        {
            int notfoundparam = 11; // starting high so the condense routine (max ten) doesn't run if we don't find it.
            LLUUID[] testateManagers = GetEstateManagers(); // temporary estate managers list


            int i = 0;
            int foundpos = notfoundparam;

            // search for estate manager.
            for (i = 0; i < testateManagers.Length; i++)
            {
                if (testateManagers[i] == avatarID)
                {
                    foundpos = i;
                    break;
                }
            }
            if (foundpos < notfoundparam)
            {
                LLUUID[] restateManagers = new LLUUID[testateManagers.Length - 1];

                // fill new estate managers array up to the found spot
                for (int j = 0; j < foundpos; j++)
                    restateManagers[j] = testateManagers[j];

                // skip over the estate manager we're removing and compress
                for (int j = foundpos + 1; j < testateManagers.Length; j++)
                    restateManagers[j - 1] = testateManagers[j];

                estateManagers = restateManagers;
            }
            else
            {
                m_log.Error("[ESTATESETTINGS]: Unable to locate estate manager : " + avatarID.ToString() + " for removal");
            }
        }

        #endregion

        public void loadConfigurationOptions()
        {
            configMember.addConfigurationOption("billable_factor", ConfigurationOption.ConfigurationTypes.TYPE_FLOAT, String.Empty,
                                                "0.0", true);
            configMember.addConfigurationOption("estate_id", ConfigurationOption.ConfigurationTypes.TYPE_UINT32, String.Empty, "100",
                                                true);
            configMember.addConfigurationOption("parent_estate_id", ConfigurationOption.ConfigurationTypes.TYPE_UINT32,
                                                String.Empty, "1", true);
            configMember.addConfigurationOption("max_agents", ConfigurationOption.ConfigurationTypes.TYPE_BYTE, String.Empty, "40",
                                                true);

            configMember.addConfigurationOption("redirect_grid_x", ConfigurationOption.ConfigurationTypes.TYPE_INT32, String.Empty,
                                                "0", true);
            configMember.addConfigurationOption("redirect_grid_y", ConfigurationOption.ConfigurationTypes.TYPE_INT32, String.Empty,
                                                "0", true);
            configMember.addConfigurationOption("price_per_meter", ConfigurationOption.ConfigurationTypes.TYPE_UINT32,
                                                String.Empty, "1", true);

            configMember.addConfigurationOption("water_height", ConfigurationOption.ConfigurationTypes.TYPE_DOUBLE, String.Empty,
                                                "20.0", true);

            configMember.addConfigurationOption("estate_name", ConfigurationOption.ConfigurationTypes.TYPE_STRING,
                                                String.Empty, "TestEstate", true);
            configMember.addConfigurationOption("estate_manager_0", ConfigurationOption.ConfigurationTypes.TYPE_LLUUID,
                                                String.Empty, "00000000-0000-0000-0000-000000000000", true);
            configMember.addConfigurationOption("estate_manager_1", ConfigurationOption.ConfigurationTypes.TYPE_LLUUID,
                                                String.Empty, "00000000-0000-0000-0000-000000000000", true);
            configMember.addConfigurationOption("estate_manager_2", ConfigurationOption.ConfigurationTypes.TYPE_LLUUID,
                                                String.Empty, "00000000-0000-0000-0000-000000000000", true);
            configMember.addConfigurationOption("estate_manager_3", ConfigurationOption.ConfigurationTypes.TYPE_LLUUID,
                                                String.Empty, "00000000-0000-0000-0000-000000000000", true);
            configMember.addConfigurationOption("estate_manager_4", ConfigurationOption.ConfigurationTypes.TYPE_LLUUID,
                                                String.Empty, "00000000-0000-0000-0000-000000000000", true);
            configMember.addConfigurationOption("estate_manager_5", ConfigurationOption.ConfigurationTypes.TYPE_LLUUID,
                                                String.Empty, "00000000-0000-0000-0000-000000000000", true);
            configMember.addConfigurationOption("estate_manager_6", ConfigurationOption.ConfigurationTypes.TYPE_LLUUID,
                                                String.Empty, "00000000-0000-0000-0000-000000000000", true);
            configMember.addConfigurationOption("estate_manager_7", ConfigurationOption.ConfigurationTypes.TYPE_LLUUID,
                                                String.Empty, "00000000-0000-0000-0000-000000000000", true);
            configMember.addConfigurationOption("estate_manager_8", ConfigurationOption.ConfigurationTypes.TYPE_LLUUID,
                                                String.Empty, "00000000-0000-0000-0000-000000000000", true);
            configMember.addConfigurationOption("estate_manager_9", ConfigurationOption.ConfigurationTypes.TYPE_LLUUID,
                                                String.Empty, "00000000-0000-0000-0000-000000000000", true);
        }

        public bool handleIncomingConfiguration(string configuration_key, object configuration_result)
        {
            switch (configuration_key)
            {
                case "billable_factor":
                    m_billableFactor = (float) configuration_result;
                    break;
                case "estate_id":
                    m_estateID = (uint) configuration_result;
                    break;
                case "parent_estate_id":
                    m_parentEstateID = (uint) configuration_result;
                    break;
                case "redirect_grid_x":
                    m_redirectGridX = (int) configuration_result;
                    break;
                case "redirect_grid_y":
                    m_redirectGridY = (int) configuration_result;
                    break;
                case "price_per_meter":
                    m_pricePerMeter = Convert.ToInt32(configuration_result);
                    break;

                case "estate_name":
                    m_estateName = (string) configuration_result;
                    break;
                case "estate_manager_0":
                    m_estateManager0 = (LLUUID) configuration_result;
                    break;
                case "estate_manager_1":
                    m_estateManager1 = (LLUUID) configuration_result;
                    break;
                case "estate_manager_2":
                    m_estateManager2 = (LLUUID) configuration_result;
                    break;
                case "estate_manager_3":
                    m_estateManager3 = (LLUUID) configuration_result;
                    break;
                case "estate_manager_4":
                    m_estateManager4 = (LLUUID) configuration_result;
                    break;
                case "estate_manager_5":
                    m_estateManager5 = (LLUUID) configuration_result;
                    break;
                case "estate_manager_6":
                    m_estateManager6 = (LLUUID) configuration_result;
                    break;
                case "estate_manager_7":
                    m_estateManager7 = (LLUUID) configuration_result;
                    break;
                case "estate_manager_8":
                    m_estateManager8 = (LLUUID) configuration_result;
                    break;
                case "estate_manager_9":
                    m_estateManager9 = (LLUUID) configuration_result;
                    break;
            }

            return true;
        }
    }
}