﻿/// <reference path="DragTool.js"/>
/// <reference path="Overlay.js" />
/// <reference path="RectangleKeyFrame.js" />
/// <reference path="Page.xaml.js" />
/// <reference path="TimeBar.js" />
/// <reference path="RectangleOverlay.js" />
function RectangleTool(page)
{
    this.Page = page;
    this.Control = page.control;
    this.Overlay = page.control.content.findName("Overlay");
    this.Media = page.control.content.findName("Media");
    this.Xaml = page.XamlFiles["RectangleTool.xaml"];
    this.HandleXaml = page.XamlFiles["Handle.xaml"];
    this.MarkerXaml = page.XamlFiles["Marker.xaml"];
    this.TimeBarXaml = page.XamlFiles["TimeBar.xaml"];
    this.Drawing = false;
}

function OnRectangleKeyFrameCreated(keyFrame, context)
{
    context.KeyFrame = keyFrame;
}

function OnFailedToCreateRectangleKeyFrame(error, context)
{
    alert(error.get_message());
}

RectangleTool.prototype = 
{
    LeftButtonDown : function(sender, eventArgs)
    {
        if (sender.CaptureMouse())
        {
            Page.DeselectOverlay();

            this.RemoveTool();
            
            this.Drawing = true;
           
            var position = eventArgs.getPosition(sender);
            this.StartDragPosition = position;
            this.Shape = this.Control.content.createFromXaml(this.Xaml, true);
            this.DragTool = new DragTool(this.Shape, this.Overlay, "Rectangle");
            this.Rectangle = this.Shape.findName("Rectangle");
            this.Handles = this.Shape.findName("Handles");
            
            this.Shape["Canvas.Left"] = position.X;
            this.Shape["Canvas.Top"] = position.Y;
            this.Rectangle.Width = 0;
            this.Rectangle.Height = 0;
            
            this.Overlay.children.Add(this.Shape);
        }
    },
    MouseMove : function (sender, eventArgs)
    {
        if (this.Drawing)
        {
            var position = eventArgs.getPosition(sender);
            
            var width = position.X - this.StartDragPosition.X;
            var height = position.Y - this.StartDragPosition.Y;
            
            if (width >= 0)
            {
                this.Rectangle.Width = width;
            }
            else
            {
                this.Shape["Canvas.Left"] = position.X;
                this.Rectangle.Width = this.StartDragPosition.X - position.X;
                
            }
            
            if (height >= 0)
            {
                this.Rectangle.Height = height;
            }
            else
            {
                this.Shape["Canvas.Top"] = position.Y;
                this.Rectangle.Height = this.StartDragPosition.Y - position.Y;
            }
        }
    },
    LeftButtonUp : function(sender, eventArgs)
    {
        if (this.Drawing)
        {
            this.Drawing = false;
            sender.ReleaseMouseCapture();
            
            var position = eventArgs.getPosition(sender);
            
            if (this.StartDragPosition.X == position.X && this.StartDragPosition.Y == position.Y)
            {
                // did not move
                return;
            }
            //var rectangleOverlay = new Overlay("Overlay1");
            
    //        this.KeyFrame = new xmldocs.net.rectanglekeyframe();
    //        this.KeyFrame.TimeCode = this.Media.Position.Seconds;
    //        this.KeyFrame.Left = this.Shape["Canvas.Left"];
    //        this.KeyFrame.Top   = this.Shape["Canvas.Top"];
    //        this.KeyFrame.Width   = this.Rectangle.Width;
    //        this.KeyFrame.Height  = this.Rectangle.Height;
            var mediaOverlay = this.Overlay.findName("MediaOverlay");
            
            var name = "Overlay" +  this.Page.Overlays.length;
            this.Name = name;
            
            OnRectangleAdded(this);
            
            
        }
    },
    Activate : function()
    {
    },
    Deactivate : function()
    {
        this.RemoveTool();
    }
}

RectangleTool.prototype.RemoveTool = function()
{
    if (this.Shape)
    {
        this.Shape.getParent().children.Remove(this.Shape);
        this.Shape = null;
    }
}

RectangleTool.prototype.Update = function(shape)
{
    this.Shape["Canvas.Left"] = shape["Canvas.Left"];
    this.Shape["Canvas.Top"] = shape["Canvas.Top"];
    this.Width = shape.Width;
    this.Height = shape.Height;
    
    PositionRectangleHandles(this);
}

RectangleTool.prototype.Select = function(keyFrame)
{
    this.RemoveTool();
    
    this.Shape = this.Control.content.createFromXaml(this.Xaml, true);
    this.DragTool = new DragTool(this.Shape, this.Overlay, "Rectangle");
    this.Rectangle = this.Shape.findName("Rectangle");
    this.Handles = this.Shape.findName("Handles");
    
    this.Shape["Canvas.Left"] = keyFrame.Left;
    this.Shape["Canvas.Top"] = keyFrame.Top;
    this.Rectangle.Width = keyFrame.Width;
    this.Rectangle.Height = keyFrame.Height;
    
    this.Overlay.children.Add(this.Shape);
    
    AddRectangleHandles(this);
    
}

function OnRectangleAdded(rectangleTool)
{
    /// <summary>callback from rectangle added service</service>
    /// <param name="rectangleTool" type="RectangleTool">the Rectangle Tool</param>
    AddRectangleHandles(rectangleTool);
    
    rectangleTool.Page.AddKeyFrame.IsHitTestVisible = true;

    var overlay = new Overlay(rectangleTool.Name, new RectangleOverlay(), this.Page.Timeline);

    //var timeBar = new TimeBar(rectangleTool.Control, overlay, rectangleTool.MarkerXaml, rectangleTool.TimeBarXaml);
    
    rectangleTool.Page.SelectedOverlay = overlay;
    
    overlay.AddKeyFrame(rectangleTool.Page);
    
    //overlay.Add(new RectangleKeyFrame(rectangleTool.Page.Media.Position.seconds, rectangleTool.Shape["Canvas.Left"], rectangleTool.Shape["Canvas.Top"], rectangleTool.Rectangle.Width, rectangleTool.Rectangle.Height));
    
    rectangleTool.Page.Overlays.push(overlay);
    
    this.Page.UpdateXaml();
}

RectangleTool.prototype.OnHandleDownloaded = function(sender, eventArgs)
{
    this.HandleXaml = sender.ResponseText;
    
    AddRectangleHandles(this);
}

function AddRectangleHandles(tool)
{
    tool.UpperLeft = tool.Control.content.createFromXaml(tool.HandleXaml, true);
    tool.UpperRight = tool.Control.content.createFromXaml(tool.HandleXaml, true);
    tool.LowerLeft = tool.Control.content.createFromXaml(tool.HandleXaml, true);
    tool.LowerRight = tool.Control.content.createFromXaml(tool.HandleXaml, true);
    
    AddHandle(tool, tool.UpperLeft);
    AddHandle(tool, tool.LowerLeft);
    AddHandle(tool, tool.UpperRight);
    AddHandle(tool, tool.LowerRight);
    
    PositionRectangleHandles(tool);
}

function AddHandle(tool, handle)
{
    handle.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(tool, tool.OnHandleDown));
    handle.addEventListener("MouseMove", Silverlight.createDelegate(tool, tool.OnHandleMove));
    handle.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(tool, tool.OnHandleUp));
    
    tool.Handles.children.add(handle);
}

function PositionRectangleHandles(tool)
{
    tool.UpperRight["Canvas.Left"] = tool.Rectangle.Width;
    tool.LowerLeft["Canvas.Top"] = tool.Rectangle.Height;
    tool.LowerRight["Canvas.Left"] = tool.Rectangle.Width;
    tool.LowerRight["Canvas.Top"] = tool.Rectangle.Height;
}

function FailedToAddRectangle(arg, context)
{
    alert(arg.get_message());
}

RectangleTool.prototype.OnHandleDown = function(sender, eventArgs)
{
    if (sender.CaptureMouse())
    {
        this.Offset = eventArgs.getPosition(sender);
        this.MovingHandle = true;
        this.Start  = eventArgs.getPosition(this.Overlay);
    }
}

RectangleTool.prototype.OnHandleMove = function(sender, eventArgs)
{
    if (this.MovingHandle)
    {
        var position = eventArgs.GetPosition(this.Overlay);
        
        //position.X -= this.Shape["Canvas.Left"];
        //position.Y -= this.Shape["Canvas.Top"];
        
        if (this.UpperLeft.Equals(sender))
        {
            this.Shape["Canvas.Left"] = position.X;
            this.Shape["Canvas.Top"] = position.Y;
            
            this.Rectangle.Width += (this.Start.X - position.X);
            this.Rectangle.Height += (this.Start.Y - position.Y);
            
        }
        else if (this.LowerLeft.Equals(sender))
        {
            this.Shape["Canvas.Left"] = position.X;
            
            this.Rectangle.Width += (this.Start.X - position.X);
            this.Rectangle.Height = position.Y - this.Shape["Canvas.Top"];
        }
        else if (this.UpperRight.Equals(sender))
        {
            this.Shape["Canvas.Top"] = position.Y; 
            
            this.Rectangle.Width = position.X - this.Shape["Canvas.Left"];
            this.Rectangle.Height += (this.Start.Y - position.Y);
        }
        else if (this.LowerRight.Equals(sender))
        {
            this.Rectangle.Width = position.X - this.Shape["Canvas.Left"];
            this.Rectangle.Height = position.Y - this.Shape["Canvas.Top"];
        }
        
        this.Start  = eventArgs.getPosition(this.Overlay);
        
        PositionRectangleHandles(this);
    }
}

RectangleTool.prototype.OnHandleUp = function(sender, eventArgs)
{
    if (this.MovingHandle)
    {
        sender.ReleaseMouseCapture();
        
        this.MovingHandle = false;
    }
}
