// clsLanguage.cs
// Copyright (C) 2008 Karel Jordaan karel_jordaan@yahoo.com.au
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using GUIControlLogic.Error;
using ZANZIClient;

namespace GUIControlLogic
{
    /// <summary>
    /// clsLanguage has codebehind for frmLanguage.
    /// </summary>
    /// <remarks>None</remarks>
    public partial class clsLanguage : frmLanguage, IDisposable
    {
        #region Variable declaration

        /// <summary>
        /// Reference to the calling object, the parent and MDI parent
        /// </summary>
        /// <remarks>None</remarks>
        private readonly clsMain m_cls;

        /// <summary>
        /// Reference to the IO class
        /// </summary>
        /// <remarks>To list the satelite asseblies avaiable</remarks>
        private readonly clsFileIO objIO = new clsFileIO();

        /// <summary>
        /// Referenc to the localization class
        /// </summary>
        /// <remarks>None</remarks>
        private readonly clsLocalization objLoc = new clsLocalization();

        /// <summary>
        /// Temp array to store locale codes
        /// </summary>
        /// <remarks>None</remarks>
        private string[] tempCodes;

        /// <summary>
        /// Temp array to store locale names
        /// </summary>
        /// <remarks>For display in the dropdown box=</remarks>
        private string[] tempNames;

        //	private  clsTreeView  m_trv_cls;

        #endregion

        #region Functions

        /// <summary>Main entry for clsLanguage</summary>
        /// <returns>Returns nothing</returns>
        /// <param name="frm">Used to pass the MDIParent as reference</param>
        /// <remarks>clsLanguage is the constructor in the clsLanguage class.  
        /// The <paramref name="frm"/> parameter takes a reference to frmMain.
        /// <c>public clsLanguage(clsMain cls, clsTreeView trv)</c>
        /// </remarks>
        public clsLanguage(clsMain cls)
        {
            try
            {
                m_cls = cls;
                //	m_trv_cls = trv;

                //zzoi results in a error - commented
                //MdiParent = cls;

                btnOK.Click += btnOK_Click;
                base.Load += Language_Load;
            }
            catch (Exception ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
                //return tempNames;
            }
        }

        /// <summary>Gets a list of the framework Languages for the dropdown ctrl for this frm</summary>
        /// <returns>Returns a string array of lanuage codes available for the application.</returns>
        /// <remarks>None</remarks>
        public string[] GetLanguages()
        {
            try
            {
                //Get the friendly name for language
                tempCodes = objIO.GetSateliteLanguages();
                tempNames = objLoc.GetNameFromCode(tempCodes);
                return tempNames;
            }
            catch (Exception ex)
            {
                 new clsExeption(ex);
                return tempNames;
            }
        }

        /// <summary>Sets the language for the application</summary>
        /// <param name="index">The index in the array of Satelite languages</param>
        /// <returns>Returns nothing</returns>
        /// <remarks>frmAbout is the constructor in the frmAbout class.  
        /// The <paramref name="index"/> parameter takes a integer type.
        /// </remarks>
        public void SetLanguage(int index)
        {
            try
            {
                //Lookup the code
                objLoc.SetCulture(tempCodes[index]);
            }
            catch (Exception ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        #endregion

        #region Menu handlers

        /// <summary>
        /// Menu handler for the btn OK click event
        /// </summary>
        /// <param name="sender">None</param>
        /// <param name="e">None</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                //Set the new language
                SetLanguage(cboLang.SelectedIndex);
                //make this a event that trigger clsMain.ResetCulture
                m_cls.ResetCulture();
                //	m_trv_cls.ResetCulture();

                Close();
            }
            catch (Exception ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        #endregion

        #region Events

        /// <summary>Fills the dropdown with values</summary>
        /// <param name="sender">Object triggering the event</param>
        /// <param name="e">Agruments passed</param>
        private void Language_Load(object sender, EventArgs e)
        {
            try
            {
                //Fill the cboBox
                cboLang.DataSource = GetLanguages();
            }
            catch (Exception ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        #endregion

        /// <summary>
        /// Constructor for the Language class
        /// </summary>
        /// <remarks>None</remarks>
        public clsLanguage()
        {
            InitializeComponent();
        }
    }
}