using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using System.IO;
using System.Windows.Resources;
using System.IO.IsolatedStorage;


namespace HTML5App
{
    public partial class MainPage : PhoneApplicationPage
    {
        // HTML5 and CSS3 on Windows Phone: Dark and Light Styles
        // http://robtiffany.com/windows-phone/html5-and-css3-on-windows-phone-dark-and-light-styles

        // zooming and scrolling
        // http://www.scottlogic.co.uk/blog/colin/2011/11/suppressing-zoom-and-scroll-interactions-in-the-windows-phone-7-browser-control/#comment-96355

        public MainPage()
        {
            InitializeComponent();
            webBrowser.Opacity = 0;
            btn1.Opacity = 0;

            // TODO: does not work?
            //SetBrowserBackground();

            // Store all HTML, Javascript, CSS and image files into isolated storage and use them for there
            // http://msdn.microsoft.com/en-us/library/ff431811%28v=vs.92%29.aspx
            SaveFilesToIsoStore();

            //webBrowser.Navigate(new Uri("html_content/metroIndex.html", UriKind.Relative));
            webBrowser.Navigate(new Uri("html_content/jqueryIndex.html", UriKind.Relative));

            // Listening javascript notifications from HTML
            webBrowser.ScriptNotify += new EventHandler<NotifyEventArgs>(webBrowser_ScriptNotify);
        }

        // Change brower background to same than application
        private void SetBrowserBackground()
        {
            Color mc =
              (Color)Application.Current.Resources["PhoneBackgroundColor"];
            webBrowser.Background = new SolidColorBrush(mc);
        }

        void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
        {
            MessageBox.Show(e.Value);
        }

        private void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            // Show browser
            webBrowser.Opacity = 1;
            btn1.Opacity = 1;

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Run javascript from C#
            webBrowser.InvokeScript("someFunction", new String[]{"C#"});
        }

        private void SaveFilesToIsoStore()
        {
            //These files must match what is included in the application package,
            //or BinaryStream.Dispose below will throw an exception.
            string[] files = {
            "html_content/metroIndex.html",
            "html_content/jqueryIndex.html",
            "html_content/script.js",
            "html_content/MetroDark.css",
            "html_content/logo.png",
            "html_content/jquery.mobile-1.0.1.css",
            "html_content/jquery.mobile.structure-1.0.1.css",
            "html_content/jquery-1.6.4.js",
            "html_content/jquery.mobile-1.0.1.js"
            };

            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            //if (false == isoStore.FileExists(files[0]))
            //{
                foreach (string f in files)
                {
                    StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
                    using (BinaryReader br = new BinaryReader(sr.Stream))
                    {
                        byte[] data = br.ReadBytes((int)sr.Stream.Length);
                        SaveToIsoStore(f, data);
                    }
                }
            //}
        }

        private void SaveToIsoStore(string fileName, byte[] data)
        {
            string strBaseDir = string.Empty;
            string delimStr = "/";
            char[] delimiter = delimStr.ToCharArray();
            string[] dirsPath = fileName.Split(delimiter);

            //Get the IsoStore.
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            //Re-create the directory structure.
            for (int i = 0; i < dirsPath.Length - 1; i++)
            {
                strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
                isoStore.CreateDirectory(strBaseDir);
            }

            //Remove the existing file.
            if (isoStore.FileExists(fileName))
            {
                isoStore.DeleteFile(fileName);
            }

            //Write the file.
            using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
            {
                bw.Write(data);
                bw.Close();
            }
        }

    }
}