using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Dialogs;
using Microsoft.WindowsAPICodePack.Dialogs.Controls;
using FooEditor.Properties;

namespace FooEditor
{
    abstract public class CustomFileDialog : IDisposable
    {
        protected SortedList<string, int> list = new SortedList<string, int>();

        public CustomFileDialog()
        {
            foreach (EncodingInfo info in Encoding.GetEncodings())
            {
                if (list.ContainsKey(info.DisplayName) == false)
                    list.Add(info.DisplayName, info.CodePage);
            }
        }

        public string FileName
        {
            get;
            protected set;
        }

        public Encoding FileEncoding
        {
            get;
            set;
        }

        public abstract string InitialDirectory
        {
            set;
        }

        public abstract string Title
        {
            set;
        }

        public abstract void Dispose();
        public abstract CommonFileDialogResult ShowDialog();
        public abstract void addFilter(string show, string ext);
    }

    public class CustomOpenFileDialogForVista : CustomFileDialog
    {
        CommonOpenFileDialog ofd;

        public override string InitialDirectory
        {
            set { ofd.InitialDirectory = value;}
        }

        public override string Title
        {
            set { ofd.Title = value; }
        }

        public CustomOpenFileDialogForVista()
        {
            ofd = new CommonOpenFileDialog();
        }

        public override void addFilter(string show, string ext)
        {
            ofd.Filters.Add(new CommonFileDialogFilter(show,ext));
        }

        public override CommonFileDialogResult ShowDialog()
        {
            CommonFileDialogComboBox comboBox = new CommonFileDialogComboBox("comboBox1");
            int i = 0;
            foreach (KeyValuePair<string, int> kv in list)
            {
                comboBox.Items.Add(new CommonFileDialogComboBoxItem(kv.Key));
                if (this.FileEncoding != null && kv.Value == this.FileEncoding.CodePage)
                    comboBox.SelectedIndex = i;
                i++;
            }
            comboBox.Enabled = false;
            ofd.Controls.Add(new CommonFileDialogLabel(Resources.FileDialogCodepageLabel));
            ofd.Controls.Add(comboBox);

            CommonFileDialogCheckBox chekckBox = new CommonFileDialogCheckBox("CheckBox1");
            chekckBox.CheckedChanged += new EventHandler(chekckBox_CheckedChanged);
            chekckBox.Text = Resources.FileDialogCheckBox;
            chekckBox.IsChecked = true;
            ofd.Controls.Add(chekckBox);

            if (ofd.ShowDialog() == CommonFileDialogResult.Ok)
            {
                if (chekckBox.IsChecked != true)
                {
                    int SelectedCodePage = list[comboBox.Items[comboBox.SelectedIndex].Text];
                    this.FileEncoding = Encoding.GetEncoding(SelectedCodePage);
                }else
                    this.FileEncoding = EncodeDetect.DectingEncode.GetCode2(ofd.FileName);
                this.FileName = ofd.FileName;
                return CommonFileDialogResult.Ok;
            }
            else
                return CommonFileDialogResult.Cancel;
        }

        public override void Dispose()
        {
            ofd.Dispose();
        }

        void chekckBox_CheckedChanged(object sender, EventArgs e)
        {
            CommonFileDialogCheckBox checkbox = (CommonFileDialogCheckBox)sender;
            ofd.Controls["comboBox1"].Enabled = !checkbox.IsChecked;
        }
    }

    public class CustomSaveFileDialogForVista : CustomFileDialog
    {
        CommonSaveFileDialog sfd;

        public override string InitialDirectory
        {
            set { sfd.InitialDirectory = value; }
        }

        public override string Title
        {
            set { sfd.Title = value; }
        }

        public CustomSaveFileDialogForVista()
        {
            sfd = new CommonSaveFileDialog();
        }

        public override void addFilter(string show, string ext)
        {
            sfd.Filters.Add(new CommonFileDialogFilter(show,ext));
        }

        public override CommonFileDialogResult ShowDialog()
        {
            CommonFileDialogComboBox comboBox = new CommonFileDialogComboBox("comboBox1");
            int i = 0;
            foreach (KeyValuePair<string,int> kv in list)
            {
                comboBox.Items.Add(new CommonFileDialogComboBoxItem(kv.Key));
                if (kv.Value == this.FileEncoding.CodePage)
                    comboBox.SelectedIndex = i;
                i++;
            }
            sfd.Controls.Add(new CommonFileDialogLabel(Resources.FileDialogCodepageLabel));
            sfd.Controls.Add(comboBox);

            if (sfd.ShowDialog() == CommonFileDialogResult.Ok)
            {
                int SelectedCodePage = list[comboBox.Items[comboBox.SelectedIndex].Text];
                this.FileEncoding = Encoding.GetEncoding(SelectedCodePage);
                this.FileName = sfd.FileName;
                return CommonFileDialogResult.Ok;
            }
            else
                return CommonFileDialogResult.Cancel;
        }
        public override void Dispose()
        {
            sfd.Dispose();
        }
    }
}
