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.Diagnostics;

using C1.Silverlight.Excel;

namespace HelloWord
{
    public partial class MainPage : UserControl
    {
        C1XLBook _book;

        public MainPage()
        {
            InitializeComponent();
        }

        private void _btnSave_Click(object sender, RoutedEventArgs e)
        {
            Debug.Assert(_book != null);
            var dlg = new SaveFileDialog();
            dlg.Filter = "Excel Files (*.xlsx)|*.xlsx";
            if (dlg.ShowDialog().Value)
            {
                try
                {
                    // information
                    _lblStatus.Text = string.Format("Saving {0}...", dlg.SafeFileName);

                    // save workbook
                    using (var stream = dlg.OpenFile())
                    {
                        _book.Save(stream);
                    }
                    _lblStatus.Text = string.Format("Saved {0}", dlg.SafeFileName); ;
                }
                catch (Exception x)
                {
                    MessageBox.Show(x.Message);
                }
            }
        }

        private void _btHello_Click(object sender, RoutedEventArgs e)
        {
            // step 1: create a new workbook
            _book = new C1XLBook();

            // step 2: get the sheet that was created by default, give it a name
            XLSheet sheet = _book.Sheets[0];
            sheet.Name = "Hello World";

            // step 3: create styles for odd and even values
            XLStyle styleOdd = new XLStyle(_book);
            styleOdd.Font = new XLFont("Tahoma", 9, false, true);
            styleOdd.ForeColor = Color.FromArgb(255, 0, 0, 255);
            XLStyle styleEven = new XLStyle(_book);
            styleEven.Font = new XLFont("Tahoma", 9, true, false);
            styleEven.ForeColor = Color.FromArgb(255, 255, 0, 0);

            // step 3: write content and format into some cells
            for (int i = 0; i < 100; i++)
            {
                XLCell cell = sheet[i, 0];
                cell.Value = i + 1;
                cell.Style = ((i + 1) % 2 == 0) ? styleEven : styleOdd;
            }

            // step 4: allow save the file
            _lblStatus.Text = "You can save workbook";
            _btnSave.IsEnabled = true;
        }

        private void _btDefault_Click(object sender, RoutedEventArgs e)
        {
            // create book (will have a single sheet by default)
            C1XLBook book = new C1XLBook();

            MessageBox.Show(string.Format("The book has {0} sheet", book.Sheets.Count));

            // add three blank sheets
            for (int i = 0; i < 3; i++)
                book.Sheets.Add();

            MessageBox.Show(string.Format("The book now has {0} sheets", book.Sheets.Count));
        }
    }
}
