using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Vortex;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace Vortex.TutorialBrowser {

    public partial class BrowserForm : Form {
        ContentEngine m_Engine;

        public BrowserForm () {
            InitializeComponent();
        }

        private void BrowserForm_Load (object sender, EventArgs e) {
            m_Engine = new ContentEngine("browser");
            ContentBrowser.DocumentText = m_Engine.BuildPage();
        }

        private void ContentBrowser_Navigating (object sender, WebBrowserNavigatingEventArgs e) {
            string id = e.Url.Host;
            switch (e.Url.Scheme) {
                case "run":
                    RunItem(e.Url.Host);
                    e.Cancel = true;
                    break;
                case "web":
                    OpenURLInDefaultBrowser(e.Url.ToString().Replace("web://", "http://"));
                    e.Cancel = true;
                    break;
            }
        }

        private void RunItem (string itemId) {
            ContentItemInfo contentItem = m_Engine.GetItemById(itemId);
            
            this.Hide();

#if DEBUG
            Tutorials.Launcher.Launch(itemId);
            this.Close();
            return;
#else
            try {
                ProcessStartInfo psi = new ProcessStartInfo();
                if (null != contentItem.ExecPath) {
                    FileInfo fi = new FileInfo(contentItem.ExecPath);
					psi.FileName = fi.FullName;
                    psi.WorkingDirectory = fi.Directory.FullName;
                } else {
					FileInfo file = new FileInfo(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName.Replace(".vshost", ""));
					psi.FileName = file.FullName;
                    psi.Arguments = itemId;
                }

                Process proc = Process.Start(psi);
                proc.WaitForExit();

            } catch (Exception) {
                this.Show();
                MessageBox.Show("Sorry but specified item is not accessible", "Can find tutorial/demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
#endif

            this.Show();
            this.Activate();
        }

        private void OpenURLInDefaultBrowser (string url) {
            System.Diagnostics.Process.Start(url);
        }

        private void ProjectHomeLink_LinkClicked (object sender, LinkLabelLinkClickedEventArgs e) {
            OpenURLInDefaultBrowser("http://vortex2d.codeplex.com/");
        }

        private void DocumentationLink_LinkClicked (object sender, LinkLabelLinkClickedEventArgs e) {
            OpenURLInDefaultBrowser("http://vortex2d.codeplex.com/documentation");
        }

        private void DevelopmentBlogLink_LinkClicked (object sender, LinkLabelLinkClickedEventArgs e) {
            OpenURLInDefaultBrowser("http://www.vortex2d.net");
        }

        private void ForumLink_LinkClicked (object sender, LinkLabelLinkClickedEventArgs e) {
            OpenURLInDefaultBrowser("http://forum.vortex2d.net");
        }

        private void LicenseLink_LinkClicked (object sender, LinkLabelLinkClickedEventArgs e) {
            OpenURLInDefaultBrowser("http://vortex2d.codeplex.com/license");
        }

		private void EngineFAQLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
			OpenURLInDefaultBrowser("http://vortex2d.codeplex.com/wikipage?title=Vortex%20FAQ");
		}


        private void ContentBrowser_DocumentCompleted (object sender, WebBrowserDocumentCompletedEventArgs e) {

        }

		private void ContentFilter_Changed(object sender, EventArgs e) {
			UpdateFilters();
		}

		private void UpdateFilters() {
			ContentBrowser.Document.InvokeScript("applyFilters", new object[] { MakeFilter() });
		}

		private string MakeFilter() {
			List<string> filters = new List<string>();
			filters.Add("basic : " + basicTutorialsFilter.Checked);
			filters.Add("advanced : " + advancedTutorialsFilter.Checked);
			filters.Add("demo : " + richDemosFilter.Checked);
			return ("{" + string.Join(", ", filters.ToArray()) + "}").ToLower() ;
		}
    }

}
