using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using C1.Xaml.Word;
using C1.Xaml.Word.Objects;
using C1.Xaml.RichTextBox;
using C1.Xaml.RichTextBox.Documents;

namespace WordSamples
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class TextFlowPage : Page
    {
        C1WordDocument _doc;

        public TextFlowPage()
        {
            this.InitializeComponent();
            this.Loaded += TextFlowPage_Loaded;

            _doc = new C1WordDocument(PaperKind.Letter);
            _doc.Clear();
        }

        async void TextFlowPage_Loaded(object sender, RoutedEventArgs e)
        {
            progressRing.IsActive = true;
            CreateDocumentTextFlow(_doc);
            WordUtils.SetDocumentInfo(_doc, Strings.TextFlowDocumentTitle);
            c1RichTextBox1.Document = new RtfFilter().ConvertToDocument(_doc.ToRtfText());
            progressRing.IsActive = false;
        }

        static void CreateDocumentTextFlow(C1WordDocument word)
        {
            // load long string from resource file
            string text = Strings.ResourceNotFound;

            using (var sr = new StreamReader(typeof(BasicTextPage).GetTypeInfo().Assembly.GetManifestResourceStream("WordSamples.Resources.flow.txt")))
            {
                text = sr.ReadToEnd();
            }
            text = text.Replace("\t", "   ");
            text = string.Format("{0}\r\n\r\n---oOoOoOo---\r\n\r\n{0}", text);

            // create word document
            word.Info.Title = Strings.TextFlowDocumentTitle;

            // table
            int rows = 4;
            int cols = 2;
            RtfTable table = new RtfTable(rows, cols);
            word.Add(table);
            table.Rows[0].Cells[0].SetMerged(1, 2);

            for (int row = 0; row < rows; row++)
            {
                if (row == 0)
                {
                    table.Rows[row].Height = 50;
                }
                for (int col = 0; col < cols; col++)
                {
                    //RtfParagraph paragraph = new RtfParagraph();
                    //paragraph.Alignment = RtfHorizontalAlignment.Undefined;
                    //paragraph.Content.Add(new RtfString(string.Format("table cell {0}:{1}.", row, col)));
                    //table.Rows[row].Cells[col].Content.Add(paragraph);
                    table.Rows[row].Cells[col].Content.Add(new RtfString(string.Format("table cell {0}:{1}.", row, col)));
                    if (row == 0 && col == 0)
                    {
                        table.Rows[row].Cells[col].Alignment = ContentAlignment.MiddleCenter;
                        table.Rows[row].Cells[col].BackFilling = Colors.LightPink;
                    }
                    else
                    {
                        table.Rows[row].Cells[col].BackFilling = Colors.LightYellow;
                    }
                    table.Rows[row].Cells[col].BottomBorderWidth = 2;
                    table.Rows[row].Cells[col].TopBorderWidth = 2;
                    table.Rows[row].Cells[col].LeftBorderWidth = 2;
                    table.Rows[row].Cells[col].RightBorderWidth = 2;
                }
            }

            return;

            // add title
            Font titleFont = new Font("Tahoma", 24, RtfFontStyle.Bold);
            Font bodyFont = new Font("Tahoma", 9);
            Rect rcPage = WordUtils.PageRectangle(word);
            Rect rc = WordUtils.RenderParagraph(word, word.Info.Title, titleFont, rcPage, rcPage, false);
            rc.Y += titleFont.Size + 6;
            rc.Height = rcPage.Height - rc.Y;

            // create two columns for the text
            Rect rcLeft = rc;
            rcLeft.Width = rcPage.Width / 2 - 12;
            rcLeft.Height = 300;
            rcLeft.Y = (rcPage.Y + rcPage.Height - rcLeft.Height) / 2;
            Rect rcRight = rcLeft;
            rcRight.X = rcPage.Right - rcRight.Width;

            // start with left column
            rc = rcLeft;

            // render string spanning columns and pages
            for (; ; )
            {
                // render as much as will fit into the rectangle
                rc = WordUtils.Inflate(rc, -3, -3);
                //int nextChar = word.DrawString(text, bodyFont, Colors.Black, rc);
                word.DrawString(text, bodyFont, Colors.Black, rc);
                rc = WordUtils.Inflate(rc, +3, +3);
                word.DrawRectangle(Colors.LightGray, rc);

                // break when done
                //if (nextChar >= text.Length)
                {
                    break;
                }

                // get rid of the part that was rendered
                //text = text.Substring(nextChar);

                // switch to right-side rectangle
                if (rc.Left == rcLeft.Left)
                {
                    rc = rcRight;
                }
                else // switch to left-side rectangle on the next page
                {
                    word.PageBreak();
                    rc = rcLeft;
                }
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            WordUtils.Save(_doc);
        }
    }
}
