﻿/// <reference path="DragTool.js"/>

function PolygonTool(page)
{   
    ///<summary>Create a Polygon Tool</summary>
    ///<param name="page" type="MediaOverlayDesigner.Page">the page</param>
    ///<return type="PolygonTool">a new Polygon Tool</return>
    this.Drawing = false;
    this.Page = page;
    this.Button = page.control.content.findName("Polygon");
    this.Overlay = page.control.content.findName("Overlay");
    this.ToolXaml = page.XamlFiles["PolygonTool.xaml"];
    this.SupportToolsXaml = page.XamlFiles["PolygonSupportTools.xaml"];
    this.HandleXaml = page.XamlFiles["Handle.xaml"];
    this.MarkerXaml = page.XamlFiles["Marker.xaml"];
    this.TimeBarXaml = page.XamlFiles["TimeBar.xaml"];
    this.SupportingToolsFrame = page.control.content.findName("SupportingToolsFrame");
    this.Points = new Array();
}


PolygonTool.prototype = 
{
    Activate : function()
    {
        this.SupportingToolsFrame.children.Clear();
        this.SupportingTools = this.Page.control.content.createFromXaml(this.SupportToolsXaml, true);
        var closePolygon = this.SupportingTools.findName("ClosePolygon");
        closePolygon.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.ClosePolygon));
        this.SupportingToolsFrame.children.Add(this.SupportingTools);
    },
    Deactivate : function()
    {
        this.SupportingToolsFrame.children.Remove(this.SupportingTools);
    },
    LeftButtonDown : function(sender, eventArgs)
    {
        if (sender.CaptureMouse())
        {
            if (!this.Creating)
            {
                this.RemoveTool();
            }
          
            this.Drawing = true;
            var point = eventArgs.GetPosition(this.Overlay);
            if (this.Tool == null)
            {
                this.Tool = this.Page.control.content.createFromXaml(this.ToolXaml, true);
                this.Creating = true;
                this.DragTool = new DragTool(this.Tool, this.Overlay, "Polygon");
                
                this.Path = this.Tool.findName("Path");
                this.Handles = this.Tool.findName("Handles");
                this.Path.StartPoint = point;
                
                this.Overlay.children.Add(this.Tool);
                
                this.Points = new Array();
                
                this.Points.push(point);
            }
            
            var newSegment = '<LineSegment/>';
            
            this.CurrentSegment = this.Page.control.content.createFromXaml(newSegment, true);
            this.CurrentSegment.Point = point;
            
            this.Path.Segments.Add(this.CurrentSegment);
            
            this.Points.push(point);
        }
    },
    MouseMove : function (sender, eventArgs)
    {
        if (this.Drawing)
        {
            var point = eventArgs.GetPosition(this.Overlay);
            this.CurrentSegment.Point = point;
            this.Points[this.Points.length - 1] = point;
        }
    },
    LeftButtonUp : function(sender, eventArgs)
    {
        if (this.Drawing)
        {
            sender.ReleaseMouseCapture();
            
            this.Drawing = false;
        }
    }
}

PolygonTool.prototype.toString = function()
{
    return "PolygonTool";
}

PolygonTool.prototype.Update = function(shape)
{
    this.Tool["Canvas.Left"] = shape["Canvas.Left"];
    this.Tool["Canvas.Top"] = shape["Canvas.Top"];
    //this.Width = shape.Width;
    //this.Height = shape.Height;
    
    PositionPolygonHandles(this);
}

PolygonTool.prototype.Select=function(keyFrame)
{
    this.RemoveTool();
    
    this.Tool = this.Page.control.content.createFromXaml(this.ToolXaml, true);
    this.DragTool = new DragTool(this.Tool, this.Overlay, "Polygon");
                
    this.Path = this.Tool.findName("Path");
    this.Handles = this.Tool.findName("Handles");
    this.Path.StartPoint = keyFrame.Points[0];
    this.Points = new Array();
    
    this.Points.push(keyFrame.Points[0]);
            
    var newSegment = '<LineSegment/>';

    for (var i = 1; i < keyFrame.Points.length; i++)
    {
        var segment = this.Page.control.content.createFromXaml(newSegment, true);
        
        segment.Point = keyFrame.Points[i];
            
        this.Path.Segments.Add(segment);
        
        this.Points.push(keyFrame.Points[i]);    
    }
    
    this.Overlay.children.Add(this.Tool);
    
    this.AddHandles();
}



PolygonTool.prototype.RemoveTool = function()
{
   if (this.Tool)
    {
        this.Tool.getParent().children.Remove(this.Tool);
        this.Tool = null;
    }
}


PolygonTool.prototype.ClosePolygon = function(sender, eventArgs)
{
    this.AddHandles();
    
    this.Creating = false;
    
    //this.Tool = null;
    
    this.Page.AddKeyFrame.IsHitTestVisible = true;
    
    var mediaOverlay = this.Overlay.findName("MediaOverlay");
    
    this.Name = "Overlay" +  this.Page.Overlays.length;
    
    var overlay = new Overlay(this.Name, new PolygonOverlay(), this.Page.Timeline);

    //var timeBar = new TimeBar(this.Page.control, overlay, this.MarkerXaml, this.TimeBarXaml);
    
    overlay.AddKeyFrame(this.Page); //.Add(new PolygonKeyFrame(this.Page.Media.Position.seconds, this.Points));
    
    this.Page.Overlays.push(overlay);
    this.Page.SelectedOverlay = overlay;
    
    this.Page.UpdateXaml();
    
    //this.Points = new Array();
    
}

PolygonTool.prototype.AddHandles = function()
{
    for (var i = 0; i < this.Points.length; i++)
    {
        var handle = this.Page.control.content.createFromXaml(this.HandleXaml, true);

        handle.Tag = i.toString();        
        handle.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.OnHandleDown));
        handle.addEventListener("MouseMove", Silverlight.createDelegate(this, this.OnHandleMove));
        handle.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.OnHandleUp));
        
       var point = this.Points[i];
        
        handle["Canvas.Left"] = point.X;
        handle["Canvas.Top"] = point.Y;
        
        this.Handles.children.Add(handle);
    }
}

PolygonTool.prototype.OnHandleDown = function(sender, eventArgs)
{
    if (sender.CaptureMouse())
    {
        //this.Offset = eventArgs.getPosition(sender);
        this.MovingHandle = true;
        //this.Start  = eventArgs.getPosition(this.Overlay);
    }
}

PolygonTool.prototype.OnHandleMove = function(sender, eventArgs)
{
    if (this.MovingHandle)
    {
        var position = eventArgs.GetPosition(this.Overlay);
        
        position.X -= this.Tool["Canvas.Left"];
        position.Y -= this.Tool["Canvas.Top"];
        var index = Number(sender.Tag);
        
        this.Points[index] = position;
        
        if (index == 0)
        {
            this.Path.StartPoint = position;
        }
        else
        {
            this.Path.Segments.getItem(index - 1).Point = position;
        }
        
        PositionPolygonHandles(this);
    }
}

function PositionPolygonHandles(tool)
{
    for (var i = 0; i < tool.Points.length; i++)
    {
        var point = tool.Points[i];
        
        var handle = tool.Handles.children.GetItem(i);
        
        handle["Canvas.Left"] = point.X;
        handle["Canvas.Top"] = point.Y;
    }
}

PolygonTool.prototype.OnHandleUp = function(sender, eventArgs)
{
    if (this.MovingHandle)
    {
        sender.ReleaseMouseCapture();
        
        this.MovingHandle = false;
    }
}
