using BookmarkManager;
using bzit.bomg.Models;
using FizzyLauncher.Models;
using RyzStudio;
using RyzStudio.IO;
using RyzStudio.Windows.Forms;
using RyzStudio.Windows.ThemedForms;
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FizzyLauncher
{
    public class BookmarkForm : TDialogForm
    {
        private System.Windows.Forms.Label label1;
        private TButton button1;
        private RyzStudio.Windows.Forms.THorizontalSeparator tHorizontalSeparator1;
        private TTextBox textBox1;
        private RyzStudio.Windows.Forms.THorizontalSeparator tHorizontalSeparator2;
        private Label label2;
        private Label label3;
        private TMemoBox memoBox1;
        private TMemoBox memoBox2;
        private Label label4;
        private TButtonTextBox textBox2;
        private PictureBox pictureBox1;
        private PictureBox pictureBox2;

        private ToolTip toolTip1;
        private System.ComponentModel.IContainer components;

        protected WebParser webParser = null;
        protected WebClient webClient = null;
        protected AppSession appSession = null;


        public BookmarkForm(AppSession session, BookmarkItem model, Image icon) : base()
        {
            InitializeComponent();

            appSession = session;

            textBox1.Text = model.SiteName?.Trim() ?? string.Empty;
            textBox2.Text = model.SiteAddress?.Trim() ?? string.Empty;
            textBox2.SetIcon("search");
            memoBox1.Text = model.SiteDescription?.Trim() ?? string.Empty;
            memoBox2.Text = model.Notes?.Trim() ?? string.Empty;
            pictureBox1.Image = icon;

            textBox2.SetTooltipText(toolTip1, "Retrieve web page information");
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new RyzStudio.Windows.ThemedForms.TButton();
            this.tHorizontalSeparator1 = new RyzStudio.Windows.Forms.THorizontalSeparator();
            this.tHorizontalSeparator2 = new RyzStudio.Windows.Forms.THorizontalSeparator();
            this.textBox1 = new RyzStudio.Windows.ThemedForms.TTextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.memoBox1 = new RyzStudio.Windows.ThemedForms.TMemoBox();
            this.memoBox2 = new RyzStudio.Windows.ThemedForms.TMemoBox();
            this.label4 = new System.Windows.Forms.Label();
            this.textBox2 = new RyzStudio.Windows.ThemedForms.TButtonTextBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.BackColor = System.Drawing.Color.Transparent;
            this.label1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label1.Location = new System.Drawing.Point(10, 21);
            this.label1.Margin = new System.Windows.Forms.Padding(0);
            this.label1.Name = "label1";
            this.label1.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
            this.label1.Size = new System.Drawing.Size(29, 34);
            this.label1.TabIndex = 153;
            this.label1.Text = "Title";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // button1
            //
            this.button1.AcceptButton = null;
            this.button1.ActiveImage = null;
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.BackColor = System.Drawing.Color.Transparent;
            this.button1.HoverImage = null;
            this.button1.IdleImage = null;
            this.button1.LabelText = "&Save";
            this.button1.Location = new System.Drawing.Point(241, 469);
            this.button1.Margin = new System.Windows.Forms.Padding(10, 0, 0, 10);
            this.button1.Name = "button1";
            this.button1.Padding = new System.Windows.Forms.Padding(4, 4, 3, 3);
            this.button1.Size = new System.Drawing.Size(128, 32);
            this.button1.TabIndex = 4;
            this.button1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.button1_MouseClick);
            //
            // tHorizontalSeparator1
            //
            this.tHorizontalSeparator1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.tHorizontalSeparator1.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.tHorizontalSeparator1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.tHorizontalSeparator1.BackColor = System.Drawing.Color.Transparent;
            this.tHorizontalSeparator1.Location = new System.Drawing.Point(10, 437);
            this.tHorizontalSeparator1.Margin = new System.Windows.Forms.Padding(5, 0, 5, 10);
            this.tHorizontalSeparator1.MaximumSize = new System.Drawing.Size(4920, 2);
            this.tHorizontalSeparator1.MinimumSize = new System.Drawing.Size(0, 22);
            this.tHorizontalSeparator1.Name = "tHorizontalSeparator1";
            this.tHorizontalSeparator1.Padding = new System.Windows.Forms.Padding(0, 10, 0, 10);
            this.tHorizontalSeparator1.Size = new System.Drawing.Size(364, 22);
            this.tHorizontalSeparator1.TabIndex = 188;
            this.tHorizontalSeparator1.TabStop = false;
            //
            // tHorizontalSeparator2
            //
            this.tHorizontalSeparator2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.tHorizontalSeparator2.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.tHorizontalSeparator2.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.tHorizontalSeparator2.BackColor = System.Drawing.Color.Transparent;
            this.tHorizontalSeparator2.Location = new System.Drawing.Point(10, 223);
            this.tHorizontalSeparator2.Margin = new System.Windows.Forms.Padding(5, 0, 5, 10);
            this.tHorizontalSeparator2.MaximumSize = new System.Drawing.Size(4920, 2);
            this.tHorizontalSeparator2.MinimumSize = new System.Drawing.Size(0, 22);
            this.tHorizontalSeparator2.Name = "tHorizontalSeparator2";
            this.tHorizontalSeparator2.Padding = new System.Windows.Forms.Padding(0, 10, 0, 10);
            this.tHorizontalSeparator2.Size = new System.Drawing.Size(364, 22);
            this.tHorizontalSeparator2.TabIndex = 190;
            this.tHorizontalSeparator2.TabStop = false;
            //
            // textBox1
            //
            this.textBox1.AcceptButton = null;
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.BackColor = System.Drawing.Color.Transparent;
            this.textBox1.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.textBox1.Location = new System.Drawing.Point(110, 20);
            this.textBox1.Margin = new System.Windows.Forms.Padding(10, 0, 0, 10);
            this.textBox1.Name = "textBox1";
            this.textBox1.Padding = new System.Windows.Forms.Padding(4, 4, 3, 3);
            this.textBox1.ReadOnly = false;
            this.textBox1.Size = new System.Drawing.Size(222, 34);
            this.textBox1.TabIndex = 0;
            this.textBox1.UseSystemPasswordChar = false;
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.BackColor = System.Drawing.Color.Transparent;
            this.label2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label2.Location = new System.Drawing.Point(10, 67);
            this.label2.Margin = new System.Windows.Forms.Padding(0);
            this.label2.Name = "label2";
            this.label2.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
            this.label2.Size = new System.Drawing.Size(49, 34);
            this.label2.TabIndex = 193;
            this.label2.Text = "Address";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.BackColor = System.Drawing.Color.Transparent;
            this.label3.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label3.Location = new System.Drawing.Point(10, 113);
            this.label3.Margin = new System.Windows.Forms.Padding(0);
            this.label3.Name = "label3";
            this.label3.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
            this.label3.Size = new System.Drawing.Size(67, 34);
            this.label3.TabIndex = 195;
            this.label3.Text = "Description";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // memoBox1
            //
            this.memoBox1.AcceptButton = null;
            this.memoBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.memoBox1.BackColor = System.Drawing.Color.Transparent;
            this.memoBox1.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.memoBox1.Location = new System.Drawing.Point(110, 112);
            this.memoBox1.Margin = new System.Windows.Forms.Padding(10, 6, 10, 6);
            this.memoBox1.Name = "memoBox1";
            this.memoBox1.Padding = new System.Windows.Forms.Padding(4, 4, 3, 3);
            this.memoBox1.ReadOnly = false;
            this.memoBox1.Size = new System.Drawing.Size(259, 105);
            this.memoBox1.TabIndex = 2;
            //
            // memoBox2
            //
            this.memoBox2.AcceptButton = null;
            this.memoBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.memoBox2.BackColor = System.Drawing.Color.Transparent;
            this.memoBox2.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.memoBox2.Location = new System.Drawing.Point(110, 251);
            this.memoBox2.Margin = new System.Windows.Forms.Padding(10, 6, 10, 6);
            this.memoBox2.Name = "memoBox2";
            this.memoBox2.Padding = new System.Windows.Forms.Padding(4, 4, 3, 3);
            this.memoBox2.ReadOnly = false;
            this.memoBox2.Size = new System.Drawing.Size(259, 180);
            this.memoBox2.TabIndex = 3;
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.BackColor = System.Drawing.Color.Transparent;
            this.label4.ForeColor = System.Drawing.SystemColors.ControlText;
            this.label4.Location = new System.Drawing.Point(10, 252);
            this.label4.Margin = new System.Windows.Forms.Padding(0);
            this.label4.Name = "label4";
            this.label4.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
            this.label4.Size = new System.Drawing.Size(38, 34);
            this.label4.TabIndex = 198;
            this.label4.Text = "Notes";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // textBox2
            //
            this.textBox2.AcceptButton = null;
            this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox2.BackColor = System.Drawing.Color.Transparent;
            this.textBox2.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.textBox2.HighlightImage = null;
            this.textBox2.Location = new System.Drawing.Point(110, 66);
            this.textBox2.Margin = new System.Windows.Forms.Padding(10, 0, 0, 10);
            this.textBox2.Name = "textBox2";
            this.textBox2.NormalImage = null;
            this.textBox2.Padding = new System.Windows.Forms.Padding(4, 4, 3, 3);
            this.textBox2.Size = new System.Drawing.Size(259, 34);
            this.textBox2.TabIndex = 1;
            this.textBox2.UseSystemPasswordChar = false;
            this.textBox2.OnButtonClick += new System.EventHandler(this.textBox2_OnButtonClick);
            //
            // pictureBox1
            //
            this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
            this.pictureBox1.ErrorImage = null;
            this.pictureBox1.InitialImage = null;
            this.pictureBox1.Location = new System.Drawing.Point(337, 21);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(32, 34);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.pictureBox1.TabIndex = 201;
            this.pictureBox1.TabStop = false;
            //
            // pictureBox2
            //
            this.pictureBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.pictureBox2.BackColor = System.Drawing.Color.Transparent;
            this.pictureBox2.ErrorImage = null;
            this.pictureBox2.InitialImage = null;
            this.pictureBox2.Location = new System.Drawing.Point(196, 469);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(32, 32);
            this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.pictureBox2.TabIndex = 202;
            this.pictureBox2.TabStop = false;
            //
            // BookmarkForm
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(384, 521);
            this.Controls.Add(this.pictureBox2);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.memoBox2);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.memoBox1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.tHorizontalSeparator2);
            this.Controls.Add(this.tHorizontalSeparator1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label1);
            this.KeyPreview = true;
            this.MinimumSize = new System.Drawing.Size(400, 560);
            this.Name = "BookmarkForm";
            this.Text = "Edit Bookmark";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            textBox2.Focus();
        }


        public BookmarkResult Model
        {
            get => new BookmarkResult()
            {
                Item = new BookmarkItem()
                {
                    SiteName = textBox1.Text?.Trim() ?? string.Empty,
                    SiteAddress = textBox2.Text?.Trim() ?? string.Empty,
                    SiteDescription = memoBox1.Text?.Trim() ?? string.Empty,
                    Notes = memoBox2.Text?.Trim() ?? string.Empty
                },
                Icon = pictureBox1.Image
            };
        }

        protected new bool IsBusy
        {
            get => base.IsBusy;
            set
            {
                base.IsBusy = value;

                UIControl.SetValue(pictureBox2, (this.IsBusy ? UIResource1.loading_block : null));

                UIControl.SetEnable(textBox1, !this.IsBusy);
                UIControl.SetEnable(textBox2, !this.IsBusy);
                UIControl.SetEnable(memoBox1, !this.IsBusy);
                UIControl.SetEnable(memoBox2, !this.IsBusy);
                UIControl.SetEnable(button1, !this.IsBusy);
            }
        }


        private void button1_MouseClick(object sender, MouseEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private async void textBox2_OnButtonClick(object sender, EventArgs e)
        {
            await Task.Run(() =>
            {
                if (IsBusy) return;

                IsBusy = true;

                if (webParser == null) webParser = new WebParser();

                if (string.IsNullOrWhiteSpace(textBox1.Text))
                {
                    IsBusy = false;
                    return;
                }

                BookmarkResult rs = webParser.RetrieveDetails(textBox2.Text, appSession.IgnoreSSL);
                if (rs == null)
                {
                    IsBusy = false;
                    return;
                }

                if (rs.Item == null) rs.Item = new BookmarkItem();

                if (!string.IsNullOrWhiteSpace(rs.Item.SiteName)) UIControl.SetText(textBox1.TextBox, rs.Item.SiteName);
                if (!string.IsNullOrWhiteSpace(rs.Item.SiteDescription)) UIControl.SetText(memoBox1.TextBox, rs.Item.SiteDescription);

                if (string.IsNullOrWhiteSpace(rs.IconURL))
                {
                    UIControl.Clear(pictureBox1);
                }
                else
                {
                    UIControl.SetValue(pictureBox1, webParser.RetrieveImage(rs.IconURL));
                }

                IsBusy = false;
            });
        }

    }
}