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

namespace BlendModeOrganizer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Paint.NET User Files\BlendModesPlus";

        private void MoveRightBtn_Click(object sender, EventArgs e)
        {            
            if (BlendModesLB1.SelectedItems != null)
            {
                while (BlendModesLB1.SelectedItems.Count != 0)
                {
                    BlendModesLB2.Items.Add(BlendModesLB1.SelectedItem);
                    BlendModesLB2.SelectedIndex = BlendModesLB2.Items.Count - 1;
                    BlendModesLB1.Items.Remove(BlendModesLB1.SelectedItem);
                }
            }
        }

        private void MoveLeftBtn_Click(object sender, EventArgs e)
        {
            if (BlendModesLB2.SelectedItems != null)
            {
                while (BlendModesLB2.SelectedItems.Count != 0)
                {
                    BlendModesLB1.Items.Add(BlendModesLB2.SelectedItem);
                    BlendModesLB1.SelectedIndex = BlendModesLB1.Items.Count - 1;
                    BlendModesLB2.Items.Remove(BlendModesLB2.SelectedItem);
                }
            }
        }

        private void MoveUpBtn_Click(object sender, EventArgs e)
        {
            if (BlendModesLB2.SelectedItems != null && BlendModesLB2.SelectedIndex >= 1)
            {
                while (BlendModesLB2.SelectedItems.Count != 0)
                {
                    object temp = BlendModesLB2.SelectedItem;
                    int index = BlendModesLB2.SelectedIndex;
                    BlendModesLB2.Items.Remove(BlendModesLB2.SelectedItem);
                    BlendModesLB2.Items.Insert(index - 1, temp);
                    BlendModesLB2.SelectedItem = index - 1;
                }
            }
        }

        private void MoveDownBtn_Click(object sender, EventArgs e)
        {
            if (BlendModesLB2.SelectedItems != null && BlendModesLB2.SelectedIndex < BlendModesLB2.Items.Count - 1)
            {
                while (BlendModesLB2.SelectedItems.Count != 0)
                {
                    object temp = BlendModesLB2.SelectedItem;
                    int index = BlendModesLB2.SelectedIndex;
                    BlendModesLB2.Items.Remove(BlendModesLB2.SelectedItem);
                    BlendModesLB2.Items.Insert(index + 1, temp);
                    BlendModesLB2.SelectedItem = index + 1;
                }
            }
        }

        private void ExitBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            XmlTextWriter xtw = new XmlTextWriter(path + "\\BlendModes.xml", Encoding.UTF8);
            xtw.WriteStartDocument();
            xtw.WriteStartElement("BlendModes");

            foreach (object o in BlendModesLB2.Items)
            {
                xtw.WriteStartElement("Mode");
                xtw.WriteString(o.ToString());
                xtw.WriteEndElement();
            }

            xtw.WriteEndElement();
            xtw.WriteEndDocument();
            xtw.Close();
            if (File.Exists(path + "\\BlendModes.xml")) MessageBox.Show("Blend modes saved successfully.");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Directory.CreateDirectory(path);
        }
    }
}
