using System;

namespace FooEditEngine
{
    sealed class PrintableView : ViewBase
    {
        public PrintableView(Document doc, IPrintableTextRender r)
            : base (doc,r)
        {
        }

        public string Header
        {
            get;
            set;
        }

        public string Footer
        {
            get;
            set;
        }

        public override void Draw(Rectangle updateRect)
        {
            if (this.LayoutLines.Count == 0)
                return;

            if (this.Hilighter != null)
                this.Hilighter.Reset();

            Point pos = base.PageBound.Location;
            pos.X -= Src.X;

            int endRow = Math.Min(this.LayoutLines.Count - 1, Src.Row + this.LineCountOnScreen - 1);

            Size lineNumberSize = new Size(this.render.LineNemberWidth, this.render.TextArea.Height);

            IPrintableTextRender render = (IPrintableTextRender)this.render;

            //ヘッダーを印刷する
            if (this.Header != null && this.Header != string.Empty)
            {
                this.render.DrawString(this.Header, pos.X, pos.Y, StringAlignment.Center,this.PageBound.Size);

                pos.Y += (int)render.HeaderHeight;
            }

            //レイアウト行を印刷する
            for (int i = Src.Row; i <= endRow; i++)
            {
                double lineHeight = this.LayoutLines.GetData(i).Layout.Height;

                this.render.DrawOneLine(this.LayoutLines,i, pos.X + this.render.TextArea.X, pos.Y, null);

                if (this.DrawLineNumber)
                    this.render.DrawString(i.ToString(), this.PageBound.X + this.GetRealtiveX(AreaType.LineNumberArea), pos.Y, StringAlignment.Right, lineNumberSize);

                pos.Y += lineHeight;
            }

            //フッターを印刷する
            if (this.Footer != null && this.Footer != string.Empty)
            {
                this.render.DrawString(this.Footer, pos.X, pos.Y, StringAlignment.Center, this.PageBound.Size);

                pos.Y += (int)render.FooterHeight;
            }
        }

        public bool TryPageDown()
        {
            return base.TryScroll(this.Src.X, this.Src.Row + this.LineCountOnScreen);
        }

        protected override void CalculateClipRect()
        {
            double x, y, width, height;

            if (this.DrawLineNumber)
            {
                if (this.render.RightToLeft)
                    x = 0;
                else
                    x = this.render.LineNemberWidth + this.render.emSize.Width;
                y = 0;
                width = this.PageBound.Width - this.render.LineNemberWidth - this.render.emSize.Width;
                height = this.PageBound.Height;
            }
            else
            {
                if (this.render.RightToLeft)
                    x = 0;
                else
                    x = 0;
                y = 0;
                width = this.PageBound.Width;
                height = this.PageBound.Height;
            }

            if (width < 0)
                width = 0;

            if (height < 0)
                height = 0;

            this.render.TextArea = new Rectangle(x, y, width, height);
        }

        protected override void CalculateLineCountOnScreen()
        {
            if (this.LayoutLines.Count == 0)
                return;

            double y = 0;
            int i = this.Src.Row;
            for (; true; i++)
            {
                LineToIndexTableData lineData = i < this.LayoutLines.Count ?
                    this.LayoutLines.GetData(i) :
                    this.LayoutLines.GetData(this.LayoutLines.Count - 1);

                ITextLayout layout = lineData.Layout;

                double lineHeight = layout.Height;

                y += lineHeight;

                if (y >= this.PageBound.Height)
                    break;
            }
            this.LineCountOnScreen = Math.Max(i - this.Src.Row - 1, 0);

            if (this.Footer != null && this.Footer != string.Empty)
                this.LineCountOnScreen--;
            if (this.Header != null && this.Header != string.Empty)
                this.LineCountOnScreen--;
        }

        enum AreaType
        {
            LineNumberArea,
            TextArea
        }

        double GetRealtiveX(AreaType type)
        {
            switch (type)
            {
                case AreaType.LineNumberArea:
                    if (this.DrawLineNumber == false)
                        throw new InvalidOperationException();
                    if (this.render.RightToLeft)
                        return this.PageBound.TopRight.X - this.render.LineNemberWidth;
                    else
                        return this.render.TextArea.X - this.render.LineNemberWidth - this.render.emSize.Width;
                case AreaType.TextArea:
                    return this.render.TextArea.X;
            }
            throw new ArgumentOutOfRangeException();
        }
    }
}
