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

namespace Engineer.Librarys.Scnr.Scripting
{
    public partial class ScriptPanel : EngineerLibrary
    {

        Scripts scripts;

        public ScriptPanel()
        {
            LibraryName = "Script Library";
            Author = "Soldier of Lite";
            TagType = "scnr";

            InitializeComponent();
        }

        public override void Run(Halo3Map.Map.Map map, int MetaIndex)
        {
            base.Run(map, MetaIndex);

            scripts = new Scripts(map, map.IO.In);
            listViewScripts.WatermarkVisible = !(scripts.scriptCount > 0);
            ListViewItem[] items = new ListViewItem[scripts.scriptCount];
            for (int i = 0; i < scripts.scriptCount; i++)
            {
                items[i] = new ListViewItem(new string[] { scripts.scripts[i].name, scripts.scripts[i].type.name, scripts.scripts[i].returnType.name });
            }
            listViewScripts.Items.Clear();
            listViewScripts.Items.AddRange(items);

            listViewScripts.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent );
            listViewScripts.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent );
            listViewScripts.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent );
        }

        public override void RefreshTag(Halo3Map.Map.Map map, int MetaIndex)
        {
            base.RefreshTag(map, MetaIndex);

            Run(map, MetaIndex);
        }

        public string GetName()
        {
            return this.LibraryName;
        }

        private void listViewScripts_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (listViewScripts.SelectedIndices.Count == 1)
            {
                try
                {
                    scripts.scripts[listViewScripts.SelectedIndices[0]].Decompile();
                    richTextBoxScript.Text = scripts.scripts[listViewScripts.SelectedIndices[0]].scriptText;
                }
                catch
                {
                    richTextBoxScript.Text = "[ERROR]";
                    MessageBox.Show("Oh man... There was a error while decompiling this script =[",
                        "=[", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

        }

    }
}
