using System;
using System.Text;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.IO;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Graphics.Printing;
using Windows.Storage;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using System.Threading.Tasks;
using FooEditEngine;
using FooEditEngine.Metro;
using EncodeDetect;

// ユーザー コントロールのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=234236 を参照してください

namespace FooEditor
{
    public sealed partial class DocumentControl : UserControl, IXmlSerializable,IDisposable
    {
        DocumentControlViewModel vm;
        public DocumentControl()
        {
            this.InitializeComponent();
            this.vm = new DocumentControlViewModel(this.TextBox);
            this.DataContext = this.vm;
            this.Updated += (s, e) => { };
            this.TextBox.Document.Update += (s, e) => {
                if (e.insertLength == 0 && e.removeLength == 0)
                    return;
                this.Updated(this, null);
            };
        }

        public event EventHandler Updated;

        public FooTextBox TextBox
        {
            get
            {
                return this.Textbox;
            }
        }

        public string Title
        {
            get
            {
                return this.vm.Title;
            }
            set
            {
                this.vm.Title = value;
            }
        }

        public FileModel CurrentFile
        {
            get
            {
                return this.vm.CurrentFile;
            }
        }

        public bool IsDirty
        {
            get
            {
                return this.vm.IsDirty;
            }
        }

        public FileType DocumentType
        {
            get
            {
                return this.vm.DocumentType;
            }
        }

        public Encoding Encode
        {
            get
            {
                return this.vm.Encode;
            }
            set
            {
                this.vm.Encode = value;
            }
        }

        public LineFeedType LineFeed
        {
            get
            {
                return this.vm.LineFeed;
            }
            set
            {
                this.vm.LineFeed = value;
            }
        }

        public bool IsHitTextArea(Point point)
        {
            return this.vm.IsHitTextArea(point);
        }

        public void Print(PrintTaskRequestedEventArgs args)
        {
            this.vm.Print(args);
        }

        public void GetData(DataRequestedEventArgs args)
        {
            this.vm.GetData(args);
        }

        public void ApplySetting(AppSettings setting)
        {
            this.vm.ApplySetting(setting);
        }

        public async Task SetDocumentType(FileType type)
        {
            await this.vm.SetDocumentType(type);
        }

        public async Task LoadFile(FileModel file, Encoding enc = null)
        {
            await this.vm.LoadFile(file, enc);
        }

        public async Task ReloadFile(Encoding enc)
        {
            await this.vm.ReloadFile(enc);
        }

        public Popup CreatePopup(Type t)
        {
            return this.vm.CreatePopup(t);
        }

        public async Task SaveFile(FileModel file)
        {
            await this.vm.SaveFile(file);
        }

        public void Undo()
        {
            this.vm.Undo();
        }

        public void Redo()
        {
            this.vm.Redo();
        }

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public async void ReadXml(System.Xml.XmlReader reader)
        {
            await this.vm.ReadXml(reader);
        }

        public async void WriteXml(System.Xml.XmlWriter writer)
        {
            await this.vm.WriteXml(writer);
        }

        public void Dispose()
        {
            this.vm.Dispose();
        }
    }
}
