/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. 
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Sun Microsystems nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package podcastfeedviewer.media;

import javafx.scene.CustomNode;
import javafx.scene.media.MediaPlayer;
import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.media.MediaView;
import javafx.scene.media.Media;
import podcastfeedviewer.Main;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import podcastfeedviewer.media.BufferSpinner;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.animation.Timeline;
import javafx.animation.Interpolator;
import javafx.scene.media.MediaError;

/**
 * @author Raghu Nair
 */

public class MediaBox extends CustomNode{

    //Media Title.
    public var mediaTitle: String;

    //media view width.
    public var mediaViewWidth: Number = 640;

    //media height
    public var mediaViewHeight: Number = 360;

    public var mediaPlayerAutoPlay: Boolean = false;

    var myPlayer: MyPlayer = MyPlayer {
        onEndOfMedia: function() {
            myPlayer.currentTime = 0s;
            myPlayer.pause();
        }
        onError:function(e:MediaError){
            loading = false;
            errorString = e.toString();
        }
    }

    //media view
    var mediaView: MediaView = MediaView {
        mediaPlayer: bind myPlayer
        preserveRatio: true
        fitWidth: bind mediaViewWidth
        fitHeight: bind mediaViewHeight
    }

    var controllBar:ControllBar = ControllBar {
        translateY: bind mediaView.layoutY + mediaViewHeight - Main.iconSize *.75
        mediaPlayer: bind myPlayer
    }
    
    var bufferSpinner:BufferSpinner = BufferSpinner{
        numArcs: bind (Main.iconSize * .75) as Integer
        radius:bind Main.iconSize *2
        translateX: bind mediaViewWidth /2 - bufferSpinner.radius
        translateY: bind (mediaView.layoutY + mediaViewHeight)/2 - bufferSpinner.radius 
    }

    public var loading = false on replace {
        if (loading) bufferSpinner.play() else bufferSpinner.stop()
    }

    public var mediaSourceURL: String on replace {

        if (mediaSourceURL != null ){
            myPlayer.media = Media{
                source:mediaSourceURL
                onError:function(e:MediaError){
                    errorString = e.toString();
                }
            }
        }
        if(mediaPlayerAutoPlay == true) {
            myPlayer.play();
        }
    }


    var background =Rectangle {
        width: bind mediaViewWidth
        height: bind mediaViewHeight
    }

    public var errorString ="" on replace {
        if ( errorString != "" ){
            loading = false;
            errorTimeline.playFromStart();
        }
    }

    var errorText:Text = Text {
        content: bind errorString;
        font: Font{ size: 20 }
        translateX: bind  (mediaViewWidth - errorText.boundsInLocal.width) / 2;
        translateY: bind  (mediaViewHeight - errorText.boundsInLocal.height) / 2;
        fill: Color.SKYBLUE
        wrappingWidth: mediaViewWidth - Main.buttonGap
    }


    public var errorTimeline:Timeline = Timeline {
        keyFrames: [
            at (0s) {
                errorText.opacity => 0.0;
            },
            at (1s) {
                errorText.opacity => 1.0 tween Interpolator.EASEBOTH;
                background.fill => Color.LIGHTSEAGREEN tween Interpolator.EASEIN;
            },
            at(30s){
                errorText.opacity => 0.0 tween Interpolator.LINEAR;
                background.fill => Color.BLACK tween Interpolator.LINEAR;
            }
        ]
    }


    override function create():Node{
        errorText.opacity = 0.0;
        Group{
            content: [
                background,
                errorText,
                mediaView,
                controllBar,
                bufferSpinner,
                // to capture input events on top.
                Rectangle {
                    width: bind mediaViewWidth
                    height: bind mediaViewHeight
                    fill: Color.TRANSPARENT

                    onMouseEntered: function(me:MouseEvent) {
                        controllBar.show.playFromStart();
                    }
                    onMouseExited: function(me:MouseEvent) {
                        controllBar.show.rate = -1.0;
                        controllBar.opacity = 0.0;
                    }
                }

                ]
        }
    }
}