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.Xml;
using System.IO;
using System.Xml.Serialization;
using AHD.SM.ASMP;

namespace UpdateCreatorEditor
{
    public partial class Form1 : Form
    {
        NetUpdate Updates = new NetUpdate();
        ASMUpdate _SelectedUpdate = null;
        public Form1()
        {
            InitializeComponent();
        }
        void RefreshUpdates()
        {
            listView1_updates.Items.Clear();
            listView_features.Items.Clear();
            foreach (ASMUpdate update in Updates.Updates)
            {
                ListViewItem item = new ListViewItem();
                item.Text = update.Name;
                item.SubItems.Add(update.Version.ToString());
                item.SubItems.Add(update.FileSize);
                listView1_updates.Items.Add(item);
            }
        }
        void RefreshUpdate()
        {
            if (_SelectedUpdate == null)
            {
                textBox1_link.Text = "";
                textBox1_name.Text = "";
                textBox1_size.Text = "";
                textBox1_version.Text = "";
                richTextBox1.Text = "";
                listView_features.Items.Clear();
            }
            else
            {
                textBox1_link.Text = _SelectedUpdate.Link;
                textBox1_name.Text = _SelectedUpdate.Name;
                textBox1_size.Text = _SelectedUpdate.FileSize;
                textBox1_version.Text = _SelectedUpdate.Version.ToString();
                richTextBox1.Text = _SelectedUpdate.Description;
                checkBox1.Checked = _SelectedUpdate.IsForASM;
                UpdateFeatures();
            }
        }
        void UpdateFeatures()
        {
            if (_SelectedUpdate == null)
            { return; }
            listView_features.Items.Clear();
            foreach (string feature in _SelectedUpdate.Features)
            {
                listView_features.Items.Add(feature);
            }
        }
        void SaveFeatures()
        {
            if (_SelectedUpdate == null)
            { return; }
            _SelectedUpdate.Features.Clear();
            for (int i = 0; i < listView_features.Items.Count; i++)
            {
                _SelectedUpdate.Features.Add(listView_features.Items[i].Text);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ASMUpdate update = new ASMUpdate();
            update.Name = "AHD Subtitles Maker Professional";
            update.Version = "5.0";
            update.Link = "http://files.ahd-subtitles-maker.webnode.com";
            update.Description = "";
            Updates.Updates.Add(update);
            RefreshUpdates();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (_SelectedUpdate == null)
            { return; }
            listView_features.Items.Add("Feature"); SaveFeatures();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (_SelectedUpdate == null)
            { return; }
            listView_features.Items.Remove(listView_features.SelectedItems[0]); SaveFeatures();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            SaveFileDialog sav = new SaveFileDialog();
            sav.Filter = "xml|*.xml";
            if (sav.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                XmlSerializer ser = new XmlSerializer(typeof(NetUpdate));
                FileStream str = new FileStream(sav.FileName, FileMode.Create, FileAccess.Write);
                ser.Serialize(str, Updates);
                str.Close();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "xml|*.xml";
            if (op.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                XmlSerializer ser = new XmlSerializer(typeof(NetUpdate));
                FileStream str = new FileStream(op.FileName, FileMode.Open, FileAccess.Read);
                Updates = (NetUpdate)ser.Deserialize(str);
                str.Close();
                RefreshUpdates();
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (_SelectedUpdate == null)
            { checkBox1.Checked = false; return; }
            _SelectedUpdate.IsForASM = checkBox1.Checked;
        }

        private void listView1_updates_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1_updates.SelectedItems.Count == 1)
            { _SelectedUpdate = Updates.Updates[listView1_updates.SelectedItems[0].Index]; }
            else
            { _SelectedUpdate = null; }
            RefreshUpdate();
        }

        private void textBox1_name_TextChanged(object sender, EventArgs e)
        {
            if (_SelectedUpdate == null)
            { textBox1_name.Text = ""; return; }
            _SelectedUpdate.Name = textBox1_name.Text;
            listView1_updates.SelectedItems[0].Text = textBox1_name.Text;
        }

        private void textBox1_version_TextChanged(object sender, EventArgs e)
        {
            if (_SelectedUpdate == null)
            { textBox1_version.Text = ""; return; }
            _SelectedUpdate.Version = textBox1_version.Text;
            listView1_updates.SelectedItems[0].SubItems[1].Text = textBox1_version.Text;
        }

        private void textBox1_size_TextChanged(object sender, EventArgs e)
        {
            if (_SelectedUpdate == null)
            { textBox1_size.Text = ""; return; }
            _SelectedUpdate.FileSize = textBox1_size.Text;
            listView1_updates.SelectedItems[0].SubItems[2].Text = textBox1_size.Text;
        }

        private void textBox1_link_TextChanged(object sender, EventArgs e)
        {
            if (_SelectedUpdate == null)
            { textBox1_link.Text = ""; return; }
            _SelectedUpdate.Link = textBox1_link.Text;
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (_SelectedUpdate == null)
            { richTextBox1.Text = ""; return; }
            _SelectedUpdate.Description = richTextBox1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1_updates.SelectedItems.Count == 1)
            {
                Updates.Updates.RemoveAt(listView1_updates.SelectedItems[0].Index);
                listView1_updates.Items.Remove(listView1_updates.SelectedItems[0]);
            }
        }

    }
}
