using EpisodeInformer.Core.Rss;
using EpisodeInformer.Core.Threading;
using EpisodeInformer.Data;
using EpisodeInformer.LocalClasses;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace EpisodeInformer
{
    public partial class frmAEDFeed : Form
    {
        private RssFeed currentFeed;

        public FeedManagerMode FormMode { get; private set; }

        public FeedAEDAction ActionTook { get; private set; }

        public frmAEDFeed()
        {
            this.InitializeComponent();
            this.FormMode = FeedManagerMode.ADD;
            this.ActionTook = FeedAEDAction.UNTOUCHED;
            this.currentFeed = new RssFeed();
            this.LoadFeeds();
            this.SetMode();
        }

        public frmAEDFeed(RssFeed feed)
        {
            this.InitializeComponent();
            this.ActionTook = FeedAEDAction.UNTOUCHED;
            this.currentFeed = feed;
            this.SetMode();
            this.cmbURL.Items.Add((object)this.currentFeed.URL);
            this.cmbURL.SelectedIndex = this.cmbURL.Items.IndexOf((object)this.currentFeed.URL);
            this.cmbURL.Text = this.currentFeed.URL;
        }

        private void frmAEDFeed_Load(object sender, EventArgs e)
        {
            ThreadingHelper.SetUIDispatcher();
            this.currentFeed.HeaderReadFinished += new delFeedHeaderRead(this.currentFeed_HeaderReadFinished);
        }

        private void btnAE_Click(object sender, EventArgs e)
        {
            if (this.FormMode == FeedManagerMode.ADD)
            {
                if (this.IsFieldsFilled())
                {
                    this.currentFeed.URL = this.cmbURL.Text;
                    this.currentFeed.Title = this.txtTitle.Text;
                    this.currentFeed.Description = this.txtDes.Text;
                    RssDBAction.AddFeed(this.currentFeed);
                    this.ActionTook = FeedAEDAction.ADDED;
                    this.cmbURL.Items.Clear();
                    this.LoadFeeds();
                    this.cmbURL.Text = "";
                    this.LockControlForValidation(false);
                    int num = (int)MessageBox.Show("Feed added to your feed list.", "Feed Added", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    int num1 = (int)MessageBox.Show("Please check URL, Title and Description fields are filled.", "Empty Fields", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else if (this.IsFieldsFilled())
            {
                this.currentFeed.URL = this.cmbURL.Text;
                this.currentFeed.Title = this.txtTitle.Text;
                this.currentFeed.Description = this.txtDes.Text;
                RssDBAction.UpdateFeed(this.currentFeed);
                this.ActionTook = FeedAEDAction.EDITED;
                this.cmbURL.Items.Clear();
                this.LoadFeeds();
                this.cmbURL.Text = "";
                this.LockControlForValidation(false);
                int num2 = (int)MessageBox.Show("Feed information updated.", "Feed Updated", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                int num3 = (int)MessageBox.Show("Please check URL, Title and Description fields are filled.", "Empty Fields", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.cmbURL.SelectedIndex == -1 || MessageBox.Show("Delete rss feed '" + this.cmbURL.Text + "' from you feed list?", "Delete feed", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                return;
            this.currentFeed.URL = this.cmbURL.Text;
            RssDBAction.DeleteFeed(this.currentFeed);
            int num = (int)MessageBox.Show("Feed deleted from feed list.", "Feed Deleted", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            this.ActionTook = FeedAEDAction.DELETED;
            this.cmbURL.Items.Clear();
            this.LoadFeeds();
            this.cmbURL.Text = "";
            this.LockControlForValidation(false);
        }

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

        private void lnkLReadHeader_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (this.cmbURL.Text.Length > 0)
            {
                this.currentFeed.URL = this.cmbURL.Text;
                this.currentFeed.ReadFeedHeaderAsync();
                this.lblStatus.Text = string.Format("Reading header of '{0}' rss feed.", (object)this.cmbURL.Text);
            }
            else
            {
                int num = (int)MessageBox.Show("You forgot to enter the feed's URL", "Missing url", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void cmbURL_TextUpdate(object sender, EventArgs e)
        {
            this.LockControlForValidation(false);
            this.FormMode = FeedManagerMode.ADD;
            this.SetMode();
        }

        private void cmbURL_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbURL.SelectedIndex != -1)
            {
                this.LockControlForValidation(true);
                this.txtTitle.Text = this.currentFeed.Title;
                this.txtDes.Text = this.currentFeed.Description;
                this.SetMode();
            }
            else
            {
                this.currentFeed.Title = (string)null;
                this.currentFeed.Description = (string)null;
                this.LockControlForValidation(false);
                this.FormMode = FeedManagerMode.ADD;
                this.SetMode();
            }
        }

        private void currentFeed_HeaderReadFinished(object sender, RssHeaderStatus status)
        {
            if (status == RssHeaderStatus.DONE)
            {
                this.lblStatus.Text = "Done reading header.";
                this.txtTitle.Text = this.currentFeed.Title;
                this.txtDes.Text = this.currentFeed.Description;
                this.LockControlForValidation(true);
            }
            else if (status == RssHeaderStatus.ERROR)
            {
                this.txtTitle.Text = "";
                this.txtDes.Text = "";
                int num = (int)MessageBox.Show("Could retrive feed header. Please check the URL and your Internet connection and try again", "Feed Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.lblStatus.Text = "Waiting";
                this.LockControlForValidation(false);
            }
            else
            {
                if (status != RssHeaderStatus.INVALID)
                    return;
                this.txtTitle.Text = "";
                this.txtDes.Text = "";
                int num = (int)MessageBox.Show("This Rss feed is not currently compatible.", "Compatibility issue", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.lblStatus.Text = "Waiting";
                this.LockControlForValidation(false);
            }
        }

        private void LoadFeeds()
        {
            foreach (RssFeed rssFeed in RssDBQuery.GetAllFeeds())
                this.cmbURL.Items.Add((object)rssFeed.URL);
        }

        private void SetMode()
        {
            if (this.FormMode != FeedManagerMode.ADD)
                return;
            this.btnAE.Text = "ADD";
            this.btnDelete.Enabled = false;
        }

        private void SetFieldValuesForCurFeed()
        {
            this.cmbURL.Text = this.currentFeed.URL;
            this.txtTitle.Text = this.currentFeed.Title;
            this.txtDes.Text = this.currentFeed.Description;
        }

        private void LockControlForValidation(bool enabled)
        {
            this.txtTitle.Enabled = enabled;
            this.txtDes.Enabled = enabled;
            if (enabled)
                return;
            this.txtDes.Text = "";
            this.txtTitle.Text = "";
        }

        public bool IsFieldsFilled()
        {
            return this.txtTitle.Text.Trim().Length > 0 && this.txtDes.Text.Trim().Length > 0 && this.cmbURL.Text.Trim().Length > 0;
        }
    }
}
