using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Halo3Map.Scripting
{
    public partial class ScriptReader : UserControl
    {
        public ScriptsData sd;
        public ScriptHolder scripts;

        public ScriptReader(Map.Map map)
        {
            InitializeComponent();
            sd = new ScriptsData(map);
            loadScripts(ref sd);
            ScriptString.readStrings(ref sd);
        }

        public void loadScripts(ref ScriptsData scriptdata)
        {
            scripts = new ScriptHolder();

            for (int i = 0; i < scriptdata.scripts.Count; i++)
            {
                ScriptHeader sh = new ScriptHeader(scriptdata.scripts[i]);
                scripts.Add(sh);
            }

            displayScripts();
        }

        public void displayScripts()
        {
            listView1.Items.Clear();
            ListViewItem[] items = new ListViewItem[scripts.Count];
            for (int i = 0; i < items.Length; i++)
            {
                items[i] = new ListViewItem(new string[] { scripts[i].name, scripts[i].type.ToString(), scripts[i].returnType.ToString() });
            }
            listView1.Items.AddRange(items);
        }

        private void btnViewScript_Click(object sender, EventArgs e)
        {
            Script s = new Script();
            s.header = scripts[listView1.SelectedItems[0].Index];
            s.scriptChunkNumber = listView1.SelectedItems[0].Index;
            s.sd = sd;
            s.Decompile();

            textBoxX1.Text = s.script;
        }

        public class ScriptHolder : CollectionBase
        {
            public enum ScriptSortMode { ordered, reverse, alphabetical, reverse_alphabetical }
            public ScriptSortMode sorted;

            public void Add(ScriptHeader sh)
            {
                this.List.Add(sh);
            }

            public ScriptHeader this[string name]
            {
                get
                {
                    for (int i = 0; i < this.List.Count; i++)
                    {
                        ScriptHeader sh = (ScriptHeader)this.List[i];
                        if (sh.name == name)
                            return sh;
                    }
                    return null;
                }
            }

            public ScriptHeader this[int i]
            {
                get
                {
                    ScriptSortMode s = sorted;
                    sort(ScriptSortMode.ordered);
                    ScriptHeader sh = (ScriptHeader)this.List[i];
                    sort(s);
                    return sh;
                }
                set
                {
                    ScriptSortMode s = sorted;
                    sort(ScriptSortMode.ordered);
                    this.List.RemoveAt(i);
                    this.List.Insert(i, value);
                    sort(s);
                }
            }

            public class ScriptSorter : IComparer<ScriptHeader>
            {
                public ScriptSortMode mode;

                public ScriptSorter(ScriptSortMode ssm)
                {
                    mode = ssm;
                }

                public int Compare(ScriptHeader x, ScriptHeader y)
                {
                    if (mode == ScriptSortMode.ordered)
                        return x.chunkNumber.CompareTo(y.chunkNumber);
                    else if (mode == ScriptSortMode.reverse)
                        return y.chunkNumber.CompareTo(x.chunkNumber);
                    else if (mode == ScriptSortMode.alphabetical)
                        return x.name.CompareTo(y.name);
                    else if (mode == ScriptSortMode.reverse_alphabetical)
                        return y.name.CompareTo(x.name);
                    return 0;
                }
            }

            public void sort(ScriptSortMode mode)
            {
                sorted = mode;
                ScriptHeader[] temp = new ScriptHeader[List.Count];
                List.CopyTo(temp, 0);
                Array.Sort(temp, new ScriptSorter(mode));
                for (int i = 0; i < temp.Length; i++)
                {
                    List[i] = temp[i];
                }
            }
        }        
    }
}
