using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using PaintDotNet.Effects;
using PaintDotNet;

namespace BlendModesPlus
{
    public class EffectPluginConfigDialog : PaintDotNet.Effects.EffectConfigDialog
    {
        private Button buttonOK;
        private GroupBox ITBGroupBox;
        private Button BrowseButton;
        private TextBox FilePathTextbox;
        private ComboBox BlendModeComboBox;
        private CheckBox SwapLayersCB;
        private RadioButton ClipboardRB;
        private RadioButton BrowseRB;
        private Button buttonCancel;

        public string xmlpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Paint.NET User Files\BlendModesPlus\BlendModes.xml";

        public EffectPluginConfigDialog()
        {
            InitializeComponent();
        }

        protected override void InitialInitToken()
        {
            theEffectToken = new EffectPluginConfigToken();
        }

        protected override void InitTokenFromDialog()
        {
            if (File.Exists(FilePathTextbox.Text))((EffectPluginConfigToken)EffectToken).LoadedImage = Surface.CopyFromBitmap(new Bitmap(Image.FromFile(FilePathTextbox.Text), EffectSourceSurface.Bounds.Size));
            ((EffectPluginConfigToken)EffectToken).FromClipboard = ClipboardRB.Checked;
            if (Clipboard.ContainsImage())((EffectPluginConfigToken)EffectToken).ClipboardImage = Surface.CopyFromBitmap(new Bitmap((Image)Clipboard.GetImage(), EffectSourceSurface.Bounds.Size));
            ((EffectPluginConfigToken)EffectToken).FilePath = FilePathTextbox.Text;
            ((EffectPluginConfigToken)EffectToken).BlendMode = BlendModeComboBox.Text;
            ((EffectPluginConfigToken)EffectToken).SwapLayers = SwapLayersCB.Checked;
        }

        protected override void InitDialogFromToken(EffectConfigToken effectToken)
        {
            EffectPluginConfigToken token = (EffectPluginConfigToken)effectToken;
            ClipboardRB.Checked = token.FromClipboard;
            FilePathTextbox.Text = token.FilePath;
            BlendModeComboBox.Text = token.BlendMode;
            SwapLayersCB.Checked = token.SwapLayers;
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(EffectPluginConfigDialog));
            this.buttonCancel = new System.Windows.Forms.Button();
            this.buttonOK = new System.Windows.Forms.Button();
            this.ITBGroupBox = new System.Windows.Forms.GroupBox();
            this.ClipboardRB = new System.Windows.Forms.RadioButton();
            this.BrowseRB = new System.Windows.Forms.RadioButton();
            this.FilePathTextbox = new System.Windows.Forms.TextBox();
            this.BrowseButton = new System.Windows.Forms.Button();
            this.BlendModeComboBox = new System.Windows.Forms.ComboBox();
            this.SwapLayersCB = new System.Windows.Forms.CheckBox();
            this.ITBGroupBox.SuspendLayout();
            this.SuspendLayout();
            // 
            // buttonCancel
            // 
            this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.buttonCancel.Location = new System.Drawing.Point(307, 121);
            this.buttonCancel.Name = "buttonCancel";
            this.buttonCancel.Size = new System.Drawing.Size(75, 23);
            this.buttonCancel.TabIndex = 1;
            this.buttonCancel.Text = "Cancel";
            this.buttonCancel.UseVisualStyleBackColor = true;
            this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
            // 
            // buttonOK
            // 
            this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.buttonOK.Location = new System.Drawing.Point(220, 121);
            this.buttonOK.Name = "buttonOK";
            this.buttonOK.Size = new System.Drawing.Size(75, 23);
            this.buttonOK.TabIndex = 2;
            this.buttonOK.Text = "OK";
            this.buttonOK.UseVisualStyleBackColor = true;
            this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
            // 
            // ITBGroupBox
            // 
            this.ITBGroupBox.Controls.Add(this.ClipboardRB);
            this.ITBGroupBox.Controls.Add(this.BrowseRB);
            this.ITBGroupBox.Controls.Add(this.FilePathTextbox);
            this.ITBGroupBox.Controls.Add(this.BrowseButton);
            this.ITBGroupBox.Location = new System.Drawing.Point(12, 12);
            this.ITBGroupBox.Name = "ITBGroupBox";
            this.ITBGroupBox.Size = new System.Drawing.Size(370, 71);
            this.ITBGroupBox.TabIndex = 3;
            this.ITBGroupBox.TabStop = false;
            this.ITBGroupBox.Text = "Image to blend...";
            // 
            // ClipboardRB
            // 
            this.ClipboardRB.AutoSize = true;
            this.ClipboardRB.Location = new System.Drawing.Point(7, 47);
            this.ClipboardRB.Name = "ClipboardRB";
            this.ClipboardRB.Size = new System.Drawing.Size(154, 17);
            this.ClipboardRB.TabIndex = 6;
            this.ClipboardRB.TabStop = true;
            this.ClipboardRB.Text = "Copy Image From Clipboard";
            this.ClipboardRB.UseVisualStyleBackColor = true;
            this.ClipboardRB.CheckedChanged += new System.EventHandler(this.ClipboardRB_CheckedChanged);
            // 
            // BrowseRB
            // 
            this.BrowseRB.AutoSize = true;
            this.BrowseRB.Checked = true;
            this.BrowseRB.Location = new System.Drawing.Point(7, 24);
            this.BrowseRB.Name = "BrowseRB";
            this.BrowseRB.Size = new System.Drawing.Size(14, 13);
            this.BrowseRB.TabIndex = 5;
            this.BrowseRB.TabStop = true;
            this.BrowseRB.UseVisualStyleBackColor = true;
            this.BrowseRB.CheckedChanged += new System.EventHandler(this.BrowseRB_CheckedChanged);
            // 
            // FilePathTextbox
            // 
            this.FilePathTextbox.Location = new System.Drawing.Point(27, 21);
            this.FilePathTextbox.Name = "FilePathTextbox";
            this.FilePathTextbox.Size = new System.Drawing.Size(256, 20);
            this.FilePathTextbox.TabIndex = 0;
            this.FilePathTextbox.TextChanged += new System.EventHandler(this.FilePathTextbox_TextChanged);
            // 
            // BrowseButton
            // 
            this.BrowseButton.Location = new System.Drawing.Point(289, 19);
            this.BrowseButton.Name = "BrowseButton";
            this.BrowseButton.Size = new System.Drawing.Size(75, 23);
            this.BrowseButton.TabIndex = 1;
            this.BrowseButton.Text = "Browse";
            this.BrowseButton.UseVisualStyleBackColor = true;
            this.BrowseButton.Click += new System.EventHandler(this.BrowseButton_Click);
            // 
            // BlendModeComboBox
            // 
            this.BlendModeComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.BlendModeComboBox.FormattingEnabled = true;
            this.BlendModeComboBox.Location = new System.Drawing.Point(12, 89);
            this.BlendModeComboBox.MaxDropDownItems = 100;
            this.BlendModeComboBox.Name = "BlendModeComboBox";
            this.BlendModeComboBox.Size = new System.Drawing.Size(370, 21);
            this.BlendModeComboBox.TabIndex = 4;
            this.BlendModeComboBox.SelectedIndexChanged += new System.EventHandler(this.BlendModeComboBox_SelectedIndexChanged);
            // 
            // SwapLayersCB
            // 
            this.SwapLayersCB.AutoSize = true;
            this.SwapLayersCB.Location = new System.Drawing.Point(12, 120);
            this.SwapLayersCB.Name = "SwapLayersCB";
            this.SwapLayersCB.Size = new System.Drawing.Size(93, 17);
            this.SwapLayersCB.TabIndex = 5;
            this.SwapLayersCB.Text = "Swap Layers?";
            this.SwapLayersCB.UseVisualStyleBackColor = true;
            this.SwapLayersCB.CheckedChanged += new System.EventHandler(this.SwapLayersCB_CheckedChanged);
            // 
            // EffectPluginConfigDialog
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
            this.ClientSize = new System.Drawing.Size(394, 156);
            this.Controls.Add(this.SwapLayersCB);
            this.Controls.Add(this.BlendModeComboBox);
            this.Controls.Add(this.ITBGroupBox);
            this.Controls.Add(this.buttonOK);
            this.Controls.Add(this.buttonCancel);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "EffectPluginConfigDialog";
            this.Text = "BlendModes Plus";
            this.Load += new System.EventHandler(this.EffectPluginConfigDialog_Load);
            this.Controls.SetChildIndex(this.buttonCancel, 0);
            this.Controls.SetChildIndex(this.buttonOK, 0);
            this.Controls.SetChildIndex(this.ITBGroupBox, 0);
            this.Controls.SetChildIndex(this.BlendModeComboBox, 0);
            this.Controls.SetChildIndex(this.SwapLayersCB, 0);
            this.ITBGroupBox.ResumeLayout(false);
            this.ITBGroupBox.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            FinishTokenUpdate();
            DialogResult = DialogResult.OK;
            this.Close();
        }

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

        private void BrowseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog FileDialog = new OpenFileDialog();
            FileDialog.AddExtension = true;
            FileDialog.Filter = "Images|*.bmp;*.gif;*.jpg;*.jpeg;*.png";
            FileDialog.Multiselect = false;
            FileDialog.ShowHelp = false;

            if (FileDialog.ShowDialog() != DialogResult.OK)
            {
                FileDialog.Dispose();
                return;
            }

            FilePathTextbox.Text = FileDialog.FileName;
            FileDialog.Dispose();
            FinishTokenUpdate();
        }

        private void FilePathTextbox_TextChanged(object sender, EventArgs e)
        {
            FinishTokenUpdate();
        }

        private void BlendModeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            FinishTokenUpdate();
        }

        private void SwapLayersCB_CheckedChanged(object sender, EventArgs e)
        {
            FinishTokenUpdate();
        }

        private void BrowseRB_CheckedChanged(object sender, EventArgs e)
        {
            if (BrowseRB.Checked == true)
            {
                if (File.Exists(FilePathTextbox.Text) == true)
                {
                    ((EffectPluginConfigToken)EffectToken).LoadedImage = Surface.CopyFromBitmap(new Bitmap(Image.FromFile(FilePathTextbox.Text), EffectSourceSurface.Bounds.Size));
                }
            }
            
            if (ClipboardRB.Checked == true)
            {
                if (Clipboard.ContainsImage() == true)
                {
                    ((EffectPluginConfigToken)EffectToken).LoadedImage = Surface.CopyFromBitmap(new Bitmap(Clipboard.GetImage(), EffectSourceSurface.Bounds.Size));
                }
                else
                {
                    ((EffectPluginConfigToken)EffectToken).LoadedImage = null;
                }
            }
            FinishTokenUpdate();
        }

        private void ClipboardRB_CheckedChanged(object sender, EventArgs e)
        {
            if (ClipboardRB.Checked == true)
            {
                if (Clipboard.ContainsImage() == true)
                {
                    ((EffectPluginConfigToken)EffectToken).LoadedImage = Surface.CopyFromBitmap(new Bitmap(Clipboard.GetImage(), EffectSourceSurface.Bounds.Size));
                }
                else
                {
                    ((EffectPluginConfigToken)EffectToken).LoadedImage = null;
                }
            }
            
            if (BrowseRB.Checked == true)
            {
                if (File.Exists(FilePathTextbox.Text) == true)
                {
                    ((EffectPluginConfigToken)EffectToken).LoadedImage = Surface.CopyFromBitmap(new Bitmap(Image.FromFile(FilePathTextbox.Text), EffectSourceSurface.Bounds.Size));
                }
            }
            FinishTokenUpdate();
        }

        private void EffectPluginConfigDialog_Load(object sender, EventArgs e)
        {
            if (File.Exists(xmlpath))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xmlpath);
                XmlNodeList xnl = doc.SelectNodes("BlendModes/Mode");
                foreach (XmlNode node in xnl)
                {
                    this.BlendModeComboBox.Items.Add(new System.Web.UI.WebControls.ListItem(node.FirstChild.InnerText, node.LastChild.InnerText));
                }
            }
            else
            {
                this.BlendModeComboBox.Items.AddRange(new object[] {
            "Normal",
            "Additive",
            "Average",
            "Blue",
            "Color",
            "Color Burn",
            "Color Dodge",
            "Cyan",
            "Darken",
            "Difference",
            "Divide",
            "Exclusion",
            "Glow",
            "Grain Extract",
            "Grain Merge",
            "Green",
            "Hard Light",
            "Hard Mix",
            "Hue",
            "Lighten",
            "Linear Burn",
            "Linear Dodge",
            "Linear Light",
            "Luminosity",
            "Magenta",
            "Multiply",
            "Negation",
            "Overlay",
            "Phoenix",
            "Pin Light",
            "Red",
            "Reflect",
            "Saturation",
            "Screen",
            "Signed Difference",
            "Soft Light",
            "Stamp",
            "Vivid Light",
            "Yellow"});
            }
        }
    }
}
