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 System.Windows.Browser;

using Weborb.Client;

namespace SilverlightPDFGen
{
    public partial class Page : UserControl
    {
        private WeborbClient pdfGenProxy;

        public Page()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler( Page_Loaded );
        }

        void Page_Loaded( object sender, RoutedEventArgs e )
        {
            generatePDFButton.IsEnabled = false;
            openPDFButton.IsEnabled = false;
            availableTemplates.ItemsSource = GetAvailableTemplates();
            availableTemplates.SelectedIndex = 0;
            pdfGenProxy = new WeborbClient( App.WeborbURL, this );
        }

        private List<Template> GetAvailableTemplates()
        {
            List<Template> templates = new List<Template>();
            templates.Add( new Template( "Select a template", null ) );
            templates.Add( new Template( "Template with Images", "TemplateWithImages.xml" ) );
            templates.Add( new Template( "Template with Text", "TemplateWithText.xml" ) );
            templates.Add( new Template( "Template with Lists", "TemplateWithLists.xml" ) );
            templates.Add( new Template( "Template multi-page layouts", "MultiPageTemplate.xml" ) );
            templates.Add( new Template( "Data Table with custom cell renderer", "DataGridWithCustomCellRendererTemplate.xml" ) );
            templates.Add( new Template( "Data Table with data embedded in the template", "DataGridWithEmbededDataTemplate.xml" ) );
            templates.Add( new Template( "Data Table with data source as method invocation", "DataGridWithRemoteDataTemplate.xml" ) );
            templates.Add( new Template( "Data Table with data source as method invocation", "FlashContentTemplate.xml" ) );
            templates.Add( new Template( "Template with form fields", "TextInputAndTextAreaTemplate.xml" ) );
            return templates;
        }

        private void availableTemplates_SelectionChanged( object sender, SelectionChangedEventArgs e )
        {
            WebClient client = new WebClient();
            Template selectedTemplate = (Template) availableTemplates.SelectedItem;

            if( selectedTemplate.TemplatePath == null )
            {
                generatePDFButton.IsEnabled = false;
                return;
            }

            Uri templateUri = new Uri( App.ServerURL + "pdftemplates/" + selectedTemplate.TemplatePath, UriKind.Absolute );
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler( client_DownloadStringCompleted );
            client.DownloadStringAsync( templateUri );
            pdfURL.Text = "";
            openPDFButton.IsEnabled = false;
        }

        void client_DownloadStringCompleted( object sender, DownloadStringCompletedEventArgs e )
        {
            templateText.Text = e.Result;
            generatePDFButton.IsEnabled = true;
        }

        private void generatePDFButton_Click( object sender, RoutedEventArgs e )
        {
            Template selectedTemplate = (Template) availableTemplates.SelectedItem;
            Responder<String> responder = new Responder<String>( GotPDF_URL, GotError );
            pdfGenProxy.Invoke(
                "com.tmc.weborb.pdf.PDFService",
                "PrintPDF",
                new string[] { selectedTemplate.TemplatePath },
                responder );
        }


        public void GotPDF_URL( String pdfURLPath )
        {
            pdfURL.Text = pdfURLPath;
            openPDFButton.IsEnabled = true;
        }

        public void GotError( Fault faultObject )
        {
            pdfURL.Text = "";
            openPDFButton.IsEnabled = false;
            HtmlPage.Window.Alert( "Got error from remote method invocation - " + faultObject.Message );
        }

        private void openPDFButton_Click( object sender, RoutedEventArgs e )
        {
            HtmlPage.Window.Navigate( new Uri( pdfURL.Text ), "_blank" );
        }
    }

    public class Template
    {
        public string TemplateName { get; set; }
        public string TemplatePath { get; set; }

        public Template( String name, String path )
        {
            TemplateName = name;
            TemplatePath = path;
        }
    }
}