/*
 * Copyright (C) 2013 FooProject
 * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
using System;

namespace FooEditEngine
{
    enum GripperPostion
    {
        BottomLeft,
        BottomRight,
    }
    class Gripper : IEquatable<Gripper>
    {
        public const int GripperWidth = 10;
        public const int HitAreaWidth = 48;

        Controller Controller;
        EditView View;
        D2DRender Render;
        GripperPostion type;
        public Gripper(Controller controller, EditView view,D2DRender render,GripperPostion type)
        {
            this.Controller = controller;
            this.View = view;
            this.Render = render;
            this.Enabled = false;
            this.type = type;
        }
        public bool Enabled
        {
            get;
            set;
        }
        public Rectangle Rectangle
        {
            get
            {
                return this.GetGripperRect(this.type, GripperWidth, GripperWidth);
            }
        }
        public bool IsHit(Point p)
        {
            Rectangle gripperRect = this.GetGripperRect(type, HitAreaWidth, HitAreaWidth);
            return this.Enabled && gripperRect.IsHit(p);
        }

        public Point AdjustPoint(Point p)
        {
            Rectangle gripperRect = this.GetGripperRect(type, HitAreaWidth, HitAreaWidth);
            
            if (gripperRect.IsHit(p))
                p.Y = gripperRect.Y - 1;
            else
                p.Y -= gripperRect.Height;

            if (p.Y < this.Render.TextArea.Y)
                p.Y = this.Render.TextArea.Y;
            return p;
        }

        public void Draw()
        {
            if(this.Enabled)
                this.DrawGripper(this.type);
        }

        void DrawGripper(GripperPostion type)
        {
            Rectangle gripperRect = GetGripperRect(type,GripperWidth,GripperWidth);
            double radius = gripperRect.Width / 2;
            Point point;
            point = new Point(gripperRect.X + radius, gripperRect.Y + radius);
            this.Render.DrawGripper(point, radius);
        }

        Rectangle GetGripperRect(GripperPostion type,int width,int height)
        {
            TextPoint tp;
            Point point;
            double radius = width / 2;
            if (type == GripperPostion.BottomRight)
                tp = this.View.LayoutLines.GetTextPointFromIndex(this.Controller.SelectionStart + this.Controller.SelectionLength);
            else if (type == GripperPostion.BottomLeft)
                tp = this.View.LayoutLines.GetTextPointFromIndex(this.Controller.SelectionStart);
            else
                throw new ArgumentOutOfRangeException();

            point = this.View.GetPostionFromTextPoint(tp);
            double lineHeight = this.View.LayoutLines.GetLayout(tp.row).Height;
            
            return new Rectangle(point.X - radius, point.Y + lineHeight, width, height);
        }

        public bool Equals(Gripper other)
        {
            return this.Rectangle == other.Rectangle;
        }
    }
}
