/*******************************************************************************
 * ADOBE CONFIDENTIAL
 *  ___________________
 *
 *  Copyright 2015 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 *  NOTICE:  All information contained herein is, and remains
 *  the property of Adobe Systems Incorporated and its suppliers,
 *  if any.  The intellectual and technical concepts contained
 *  herein are proprietary to Adobe Systems Incorporated and its
 *  suppliers and are protected by trade secret or copyright law.
 *  Dissemination of this information or reproduction of this material
 *  is strictly forbidden unless prior written permission is obtained
 *  from Adobe Systems Incorporated.
 ******************************************************************************/

package com.adobe.primetime.reference.manager;

import android.content.Context;

import com.adobe.mediacore.MediaPlayer;
import com.adobe.mediacore.metadata.Metadata;
import com.adobe.mobile.Config;
import com.adobe.primetime.reference.PrimetimeReference;
import com.adobe.primetime.reference.config.IVAConfig;

public class VAManagerOn extends VAManager {
    private IVAConfig vaConfig;
    //private VideoAnalyticsProvider videoAnalyticsProvider;
    private Context context;

    private final String LOG_TAG = com.adobe.primetime.reference.PrimetimeReference.LOG_APP_NAME
            + VAManagerOn.class.getSimpleName();

    private static final String NODE_NAME_VA_VIDEO_ID = "videoid";
    private static final String NODE_NAME_VA_VIDEO_NAME = "videoname";
    private static final String NODE_NAME_VA_VIDEO_CHAPTERS = "chapters";
    private static final String NODE_NAME_VA_VIDEO_CHAPTER_NAME = "name";
    private static final String NODE_NAME_VA_VIDEO_CHAPTER_START = "start";
    private static final String NODE_NAME_VA_VIDEO_CHAPTER_END = "end";
    private static final String NODE_NAME_VA_PLAY_NAME = "playername";
    private static final String NODE_NAME_VA_ENABLE_CHAPTER_TRACKING = "chaptertracking";
	
	private static final String NODE_NAME_VA_TRACKING_SERVER = "trackingserver"; 
    private static final String NODE_NAME_VA_PUBLISHER = "publisher";
    private static final String NODE_NAME_VA_CHANNEL = "channel";
    private static final String NODE_NAME_VA_DEBUG_LOGGING = "debuglogging";
    private static final String NODE_NAME_VA_QUIET_MODE = "quietmode";

    public VAManagerOn(IVAConfig vaConfig, Context context) {
        this.vaConfig = vaConfig;
        this.context = context;
    }

    /**
     * Destroy VA provider by detaching it from the media player
     */
    @Override
    public void destroy()
    {
        detachPlayer();
    }

    /**
     *
     * Create Video Analytics provider to track video statistics and send to the analytics server by attaching it to
     * the media player
     * @param mediaPlayer the MediaPlayer to track video statistics
     * @param resourceMetadata the Metadata attached to the MediaResource which contains the
     *                         VideoAnalytics configuration
     */
    public void createVAProvider(MediaPlayer mediaPlayer, Metadata resourceMetadata)
    {
        detachPlayer();

        // set application context in Adobe Mobile Library
        Config.setContext(context);

        if (mediaPlayer != null)
        {
            //VideoAnalyticsMetadata vaMetadata = null;

            if (resourceMetadata == null)
            {
                PrimetimeReference.logger.i(LOG_TAG, "Media Player's resource does not contain metadata. Cannot configure Video Analytics.");
                return;
            }

            /*try {
                if (resourceMetadata.containsKey(VideoAnalyticsMetadata.VIDEO_ANALYTICS_METADATA_KEY)) {
                    JSONObject vaMetadataObj = new JSONObject(resourceMetadata.getValue(VideoAnalyticsMetadata.VIDEO_ANALYTICS_METADATA_KEY));
                    vaMetadata = parseVideoAnalyticsMetadata(vaMetadataObj);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


            if (vaMetadata == null)
            {
                vaMetadata = new VideoAnalyticsMetadata();
            }

            if (!StringUtils.isEmpty(vaConfig.getVATrackingServer()))
                vaMetadata.setTrackingServer(vaConfig.getVATrackingServer());

            if (!StringUtils.isEmpty(vaConfig.getVAChannel()))
                vaMetadata.setChannel(vaConfig.getVAChannel());

            vaMetadata.setPlayerName("Adobe Primetime Reference Player");

            vaMetadata.debugLogging = vaConfig.isVADebugLoggingEnabled();

            videoAnalyticsProvider = new VideoAnalyticsProvider(context);
            videoAnalyticsProvider.setVideoAnalyticsMetadata(vaMetadata);
            videoAnalyticsProvider.attachMediaPlayer(mediaPlayer);*/

            PrimetimeReference.logger.i(LOG_TAG, "Created a new instance of VideoAnalyticsProvider");
        }
        else
        {
            PrimetimeReference.logger.w(LOG_TAG, "Failed to create VideoAnalyticsProvider as either no media player or player item was found.");
        }
    }

    /*private VideoAnalyticsMetadata parseVideoAnalyticsMetadata(JSONObject jsonObject) throws JSONException
    {

        VideoAnalyticsMetadata videoAnalyticsMetadata = new VideoAnalyticsMetadata();

        if (jsonObject != null) {
            if (jsonObject.has(NODE_NAME_VA_VIDEO_ID)) {
                videoAnalyticsMetadata.setVideoId(jsonObject.getString(NODE_NAME_VA_VIDEO_ID));
            }

            if (jsonObject.has(NODE_NAME_VA_VIDEO_NAME)) {
                videoAnalyticsMetadata.setVideoName(jsonObject.getString(NODE_NAME_VA_VIDEO_NAME));
            }

            if (jsonObject.has(NODE_NAME_VA_VIDEO_CHAPTERS)) {
                JSONArray chapters = jsonObject.getJSONArray(NODE_NAME_VA_VIDEO_CHAPTERS);

                if (chapters.length() > 0)
                {
                    List<VideoAnalyticsChapterData> va_chapters = new ArrayList<VideoAnalyticsChapterData>();

                    for (int i=0; i < chapters.length(); i++)
                    {
                        JSONObject chapter = chapters.getJSONObject(i);

                        if (chapter.has(NODE_NAME_VA_VIDEO_CHAPTER_START) && chapter.has(NODE_NAME_VA_VIDEO_CHAPTER_END)) {
                            try
                            {
                                Long startTime = Long.parseLong(chapter.getString(NODE_NAME_VA_VIDEO_CHAPTER_START));
                                Long endTime = Long.parseLong(chapter.getString(NODE_NAME_VA_VIDEO_CHAPTER_END));
                                VideoAnalyticsChapterData va_chapter = new VideoAnalyticsChapterData(startTime, endTime);

                                if (chapter.has(NODE_NAME_VA_VIDEO_CHAPTER_NAME)) {
                                    String name = chapter.getString(NODE_NAME_VA_VIDEO_CHAPTER_NAME);
                                    va_chapter.setName(name);
                                }

                                va_chapters.add(va_chapter);
                            }
                            catch (NumberFormatException e)
                            {
                                PrimetimeReference.logger.e(LOG_TAG + "::parseVideoAnalyticsMetadata", e.getMessage());
                            }
                        }
                    }

                    if (!va_chapters.isEmpty()) {
                        videoAnalyticsMetadata.setChapters(va_chapters);
                    }
                }
            }

            if (jsonObject.has(NODE_NAME_VA_PLAY_NAME)) {
                videoAnalyticsMetadata.setPlayerName(jsonObject.getString(NODE_NAME_VA_PLAY_NAME));
            }

            if (jsonObject.has(NODE_NAME_VA_ENABLE_CHAPTER_TRACKING)) {
                videoAnalyticsMetadata.setEnableChapterTracking(Boolean.parseBoolean(jsonObject.getString(NODE_NAME_VA_ENABLE_CHAPTER_TRACKING)));
            }
        }

        return videoAnalyticsMetadata;
    }*/

    private void detachPlayer()
    {
        /*if (videoAnalyticsProvider != null) {
            videoAnalyticsProvider.detachMediaPlayer();
            videoAnalyticsProvider = null;
        }*/
    }
}
