/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "../inc/multi-frames-view.h"

#include <Elementary.h>
#include <Ecore.h>

#include <stdbool.h>
#include <assert.h>

#include "../inc/main-app.h"
#include "../inc/logger.h"

// Audio:
#include "../../audio/inc/equalizer-view.h"
#include "../../audio/inc/tone-player-view.h"
#include "../../audio/inc/gles-cube-view.h"
#include "../../audio/inc/audio-inout-view.h"

// Image:
#include "../../image/inc/image-viewer-view.h"
#include "../../image/inc/gif-viewer-view.h"
#include "../../image/inc/image-converter-view.h"
#include "../../image/inc/image-flip-rotate-view.h"
#include "../../image/inc/color-converter-view.h"
#include "../../image/inc/image-resize-view.h"
#include "../../image/inc/frame-extractor-view.h"

// Recorder:
#include "../../recorder/inc/audio-recorder-view.h"
#include "../../recorder/inc/video-recorder-view.h"
#include "../../recorder/inc/camera-capture-view.h"

// Play:
#include "../../player/inc/multi-play-view.h"
#include "../../player/inc/media-player-view.h"
#include "../../player/inc/camera-player-view.h"
#include "../../player/inc/video-recorder-player-view.h"

struct _multi_frames_view
{
    Elm_Object_Item *frame_item;
    Evas_Object *navi;
    Evas_Object *layout;
    app_data *app;
};

typedef enum
{
    CLI_IMAGE,
    CLI_AUDIO,
    CLI_RECORDER,
    CLI_PLAYER,
} category_list_item_type;

typedef enum
{
    // Image:
    LI_IMAGE_VIEWER,
    LI_IMAGE_CONVERTAER,
    LI_GIF_VIEWER,
    LI_FLIP_ROTATE,
    LI_IMAGE_RESIZE,
    LI_COLOR_CONVERT,
    LI_FRAME_EXTRACTOR,

    // Audio:
    LI_TONE_PLAYER,
    LI_AUDIO_IN_OUT,
    LI_AUDIO_EQUALIZER,
    LI_GLES_CUBE_PLAYER,

    // Recorder:
    LI_CAMERA_CAPTURE,
    LI_VIDEO_RECORDER,
    LI_AUDIO_RECORDER,

    // Play:
    LI_MEDIA_PLAYER,
    LI_MULTI_PLAY,
    LI_CAMERA_AND_PLAYER,
    LI_VIDEO_RECORDER_AND_PLAYER,
} list_item_type;

#define LIST_DATA_KEY "list"

static Evas_Object *_add_main_layout(multi_frames_view *view, Evas_Object *parent);
static void _on_main_layout_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
static Evas_Object *_add_list_view(multi_frames_view *view, Evas_Object *parent);
static void _on_categoty_list_item_selected_cb(void *data, Evas_Object *obj, void *event_info);
static void _on_list_item_selected_cb(void *data, Evas_Object *obj, void *event_info);
static Elm_Object_Item *_add_category_list_item(Evas_Object *parent, multi_frames_view *view, const char *text, category_list_item_type type);
static Elm_Object_Item *_add_list_item(Evas_Object *parent, multi_frames_view *view, const char *text, list_item_type type);

static void _push_audio_frame(multi_frames_view *view);
static void _push_image_frame(multi_frames_view *view);
static void _push_recorder_frame(multi_frames_view *view);
static void _push_player_frame(multi_frames_view *view);

multi_frames_view *multi_frames_view_add(struct _app_data *app, Evas_Object *navi)
{
    multi_frames_view *view = calloc(1, sizeof(multi_frames_view));

    if(view)
    {
        view->navi = navi;
        view->app = app;
        view->layout = _add_main_layout(view, navi);

        if(view->layout)
        {
            evas_object_event_callback_add(view->layout, EVAS_CALLBACK_DEL, _on_main_layout_del_cb, view);
            view->frame_item = elm_naviframe_item_push(view->navi, "Media app", NULL, NULL, view->layout, NULL);
        }
        else
        {
            free(view);
            return NULL;
        }
    }

    return view;
}

static Evas_Object *_add_main_layout(multi_frames_view *view, Evas_Object *parent)
{
    Evas_Object *layout = _add_list_view(view, parent);

    _add_category_list_item(layout, view, "Image", CLI_IMAGE);
    _add_category_list_item(layout, view, "Audio", CLI_AUDIO);
    _add_category_list_item(layout, view, "Recorder", CLI_RECORDER);
    _add_category_list_item(layout, view, "Player", CLI_PLAYER);
    return layout;
}

static Evas_Object *_add_list_view(multi_frames_view *view, Evas_Object *parent)
{
    Evas_Object *list = elm_list_add(parent);
    evas_object_data_set(list, LIST_DATA_KEY, view);
    evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(list, EVAS_HINT_FILL, EVAS_HINT_FILL);
    evas_object_show(list);
    return list;
}

static Elm_Object_Item *_add_category_list_item(Evas_Object *parent, multi_frames_view *view, const char *text, category_list_item_type type)
{
    Elm_Object_Item *item = elm_list_item_append(parent, text, NULL, NULL, _on_categoty_list_item_selected_cb, view);
    elm_object_item_data_set(item, (void*)type);
    return item;
}

static Elm_Object_Item *_add_list_item(Evas_Object *parent, multi_frames_view *view, const char *text, list_item_type type)
{
    Elm_Object_Item *item = elm_list_item_append(parent, text, NULL, NULL, _on_list_item_selected_cb, view);
    elm_object_item_data_set(item, (void*)type);
    return item;
}

static void _on_main_layout_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
    multi_frames_view *view = data;
    free(view);
}

static void _push_audio_frame(multi_frames_view *view)
{
    Evas_Object *layout = _add_list_view(view, view->navi);

    _add_list_item(layout, view, "Tone Player", LI_TONE_PLAYER);
    _add_list_item(layout, view, "Audio In Out", LI_AUDIO_IN_OUT);
    _add_list_item(layout, view, "Audio Equalizer", LI_AUDIO_EQUALIZER);
    _add_list_item(layout, view, "Gles Cube Player", LI_GLES_CUBE_PLAYER);

    elm_naviframe_item_push(view->navi, "Media app - Audio", NULL, NULL, layout, NULL);
}

static void _push_image_frame(multi_frames_view *view)
{
    Evas_Object *layout = _add_list_view(view, view->navi);

    _add_list_item(layout, view, "Image Viewer", LI_IMAGE_VIEWER);
    _add_list_item(layout, view, "Image Converter", LI_IMAGE_CONVERTAER);
    _add_list_item(layout, view, "Gif Viewer", LI_GIF_VIEWER);
    _add_list_item(layout, view, "Flip & Rotate", LI_FLIP_ROTATE);
    _add_list_item(layout, view, "Image Resize", LI_IMAGE_RESIZE);
    _add_list_item(layout, view, "Color Converter", LI_COLOR_CONVERT);
    _add_list_item(layout, view, "Frame Extractor", LI_FRAME_EXTRACTOR);

    elm_naviframe_item_push(view->navi, "Media app - Image", NULL, NULL, layout, NULL);
}

static void _push_recorder_frame(multi_frames_view *view)
{
    Evas_Object *layout = _add_list_view(view, view->navi);

    _add_list_item(layout, view, "Camera Capture", LI_CAMERA_CAPTURE);
    _add_list_item(layout, view, "Video Recorder", LI_VIDEO_RECORDER);
    _add_list_item(layout, view, "Audio Recorder", LI_AUDIO_RECORDER);

    elm_naviframe_item_push(view->navi, "Media app - Recorder", NULL, NULL, layout, NULL);
}

static void _push_player_frame(multi_frames_view *view)
{
    Evas_Object *layout = _add_list_view(view, view->navi);

    _add_list_item(layout, view, "Media Player", LI_MEDIA_PLAYER);
    _add_list_item(layout, view, "Multi Play", LI_MULTI_PLAY);
    _add_list_item(layout, view, "Camera and Player", LI_CAMERA_AND_PLAYER);
    _add_list_item(layout, view, "Video Recorder and Player", LI_VIDEO_RECORDER_AND_PLAYER);

    elm_naviframe_item_push(view->navi, "Media app - Player", NULL, NULL, layout, NULL);
}

static void _on_categoty_list_item_selected_cb(void *data, Evas_Object *obj, void *event_info)
{
    multi_frames_view *view = evas_object_data_get(obj, LIST_DATA_KEY);

    if(!view)
    {
        assert(false);
        return;
    }

    Elm_Object_Item *item  = elm_list_selected_item_get(obj);
    elm_list_item_selected_set(item, EINA_FALSE);
    category_list_item_type type = (category_list_item_type)elm_object_item_data_get(item);

    switch(type)
    {
        case CLI_IMAGE:
            _push_image_frame(view);
            break;

        case CLI_AUDIO:
            _push_audio_frame(view);
            break;

        case CLI_RECORDER:
            _push_recorder_frame(view);
            break;

        case CLI_PLAYER:
            _push_player_frame(view);
            break;

        default:
            assert(false);
            break;
    }
}

static void _on_list_item_selected_cb(void *data, Evas_Object *obj, void *event_info)
{
    multi_frames_view *view = evas_object_data_get(obj, LIST_DATA_KEY);

    if(!view)
    {
        assert(false);
        return;
    }

    Elm_Object_Item *item  = elm_list_selected_item_get(obj);
    elm_list_item_selected_set(item, EINA_FALSE);
    list_item_type type = (list_item_type)elm_object_item_data_get(item);

    INF("Type = %d", type);
    switch(type)
    {
        // Image:
        case LI_IMAGE_VIEWER:
            image_viewer_view_add(view->app, view->navi);
            break;

        case LI_IMAGE_CONVERTAER:
            image_converter_view_add(view->app, view->navi);
            break;

        case LI_GIF_VIEWER:
            gif_viewer_view_add(view->app, view->navi);
            break;

        case LI_FLIP_ROTATE:
            image_flip_rotate_view_add(view->app, view->navi);
            break;

        case LI_IMAGE_RESIZE:
            image_resize_view_add(view->app, view->navi);
            break;

        case LI_COLOR_CONVERT:
            color_converter_view_add(view->app, view->navi);
            break;

        case LI_FRAME_EXTRACTOR:
            frame_extractor_view_add(view->app, view->navi);
            break;

        // Audio:
        case LI_TONE_PLAYER:
            tone_player_view_add(view->app, view->navi);
            break;

        case LI_AUDIO_IN_OUT:
            audio_inout_view_add(view->app, view->navi);
            break;

        case LI_AUDIO_EQUALIZER:
            equalizer_view_add(view->app, view->navi);
            break;

        case LI_GLES_CUBE_PLAYER:
            gles_cube_view_add(view->app);
            break;

        // Recorder:
        case LI_CAMERA_CAPTURE:
            camera_capture_view_add(view->app, view->navi);
            break;

        case LI_VIDEO_RECORDER:
            video_recorder_view_add(view->app, view->navi);
            break;

        case LI_AUDIO_RECORDER:
            audio_recorder_view_add(view->app, view->navi);
            break;

        // Play:
        case LI_MEDIA_PLAYER:
            media_player_view_add(view->app, view->navi);
            break;

        case LI_MULTI_PLAY:
            multi_play_view_add(view->app, view->navi);
            break;

        case LI_CAMERA_AND_PLAYER:
            camera_player_view_add(view->app, view->navi);
            break;

        case LI_VIDEO_RECORDER_AND_PLAYER:
            video_recorder_player_view_add(view->app, view->navi);
            break;

        default:
            assert(false);
            break;
    }
}

