/* This file is part of AHD Subtitles Maker Professional
   A program can create and edit subtitles

   Copyright © Ala I Hadid 2009 - 2012

   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 3 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, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace AHD.SM
{
    public partial class Frm_Settings : Form
    {
        List<ASMSettingsControl> controls = new List<ASMSettingsControl>();
        public Frm_Settings()
        {
            InitializeComponent();
            Type[] types = Assembly.GetExecutingAssembly().GetTypes();
            foreach (Type tp in types)
            {
                if (tp.IsSubclassOf(typeof(ASMSettingsControl)))
                {
                    //listBox1.Items.Add(control);//Stupid vc, it never show names at the list
                    controls.Add(Activator.CreateInstance(tp) as ASMSettingsControl);
                }
            }
            controls.Sort(new ASMSettingsControlComparer(true));
            foreach (ASMSettingsControl control in controls)
                listBox1.Items.Add(control.ToString());
            listBox1.SelectedIndex = 0;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            panel1.Controls.Clear();
            ASMSettingsControl control = controls[listBox1.SelectedIndex];
            control.Location = new Point(0, 0);
            string test = control.ToString();
            panel1.Controls.Add(control);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
        //save and apply
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (ASMSettingsControl control in controls)
                control.SaveSettings();
            Program.Settings.Save();
            Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (ASMSettingsControl control in controls)
                control.DefaultSettings();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            controls[listBox1.SelectedIndex].DefaultSettings();
        }
    }
    public class ASMSettingsControlComparer : IComparer<ASMSettingsControl>
    {
        bool AtoZ;
        public ASMSettingsControlComparer(bool AtoZ)
        {
            this.AtoZ = AtoZ;
        }
        public int Compare(ASMSettingsControl x, ASMSettingsControl y)
        {
            if (AtoZ)
                return (StringComparer.Create(System.Threading.Thread.CurrentThread.CurrentCulture, false)).Compare(x.ToString(), y.ToString());
            else
                return (-1 * (StringComparer.Create(System.Threading.Thread.CurrentThread.CurrentCulture, false)).Compare(x.ToString(), y.ToString()));
        }
    }
}
