/*
 * 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/main-app.h"
#include "../inc/multi-frames-view.h"

#include "../inc/ui-utils.h"
#include "../inc/logger.h"

#include <stdlib.h>
#include <Elementary.h>
#include <app.h>
#include <efl_extension.h>

#define MORE_BTN_KEY "XF86Send"
#define BACK_BTN_KEY "XF86Stop"

typedef struct
{
    const void *user_data;
} event_item;

// app event callbacks
static bool _on_create_cb(void *user_data);
static void _on_terminate_cb(void *user_data);
static void _on_pause_cb(void *user_data);
static void _on_resume_cb(void *user_data);
static void _on_app_control_cb(app_control_h app_control, void *user_data);

static Evas_Object *_add_naviframe(app_data *app);
static void _navi_back_cb(void *data, Evas_Object *obj, void *event_info);

app_data *app_create()
{
    app_data *app = calloc(1, sizeof(app_data));
    return app;
}

void app_destroy(app_data *app)
{
    free(app);
}

void app_reset_cbs(app_data *app)
{
    if(app)
    {
        memset(&app->cb, 0, sizeof(app_cbs));
    }
}

void app_set_cbs(app_data *app, app_cbs cb, void *user_data)
{
    if(app)
    {
        app->user_data = user_data;
        app->cb = cb;
    }
}

int app_run(app_data *app, int argc, char ** argv)
{
    if(!app)
    {
        return -1;
    }

    ui_app_lifecycle_callback_s cbs =
    {
        .create = _on_create_cb,
        .terminate = _on_terminate_cb,
        .pause = _on_pause_cb,
        .resume = _on_resume_cb,
        .app_control = _on_app_control_cb
    };

    return ui_app_main(argc, argv, &cbs, app);
}
static Evas_Object *_add_naviframe(app_data *app)
{
    Evas_Object *result = NULL;
    Evas_Object *parent = win_get_host_layout(app->win);
    if(parent)
    {
        result = ui_utils_navi_add(parent);

        if(result)
        {
            win_set_layout(app->win, result);
        }
    }
    return result;
}

static bool _on_create_cb(void *user_data)
{
    app_data *ad = user_data;

    if(!ad)
    {
        return false;
    }

    ad->win = win_create();
    if(!ad->win)
    {
        return false;
    }

    ad->navi = _add_naviframe(ad);
    eext_object_event_callback_add(ad->navi, EEXT_CALLBACK_BACK, _navi_back_cb, ad);

    if(!ad->navi)
    {
        return false;
    }

    multi_frames_view_add(ad, ad->navi);
    return true;
}

static void _navi_back_cb(void *data, Evas_Object *obj, void *event_info)
{
    app_data *app = (app_data *)data;

    if(elm_naviframe_top_item_get(app->navi) == elm_naviframe_bottom_item_get(app->navi))
    {
        if(app)
        {
            win_lower(app->win);
        }
    }
    else
    {
        elm_naviframe_item_pop(app->navi);
    }
}

static void _on_terminate_cb(void *user_data)
{
    app_data *app = user_data;

    if(!app)
    {
        return;
    }

    win_destroy(app->win);
    app->win = NULL;
}

static void _on_pause_cb(void *user_data)
{
    app_data *app = user_data;
    if(app && app->cb.pause)
    {
        app->cb.pause(app->user_data);
    }
}

static void _on_resume_cb(void *user_data)
{
    app_data *app = user_data;
    if(app && app->cb.resume)
    {
        app->cb.resume(app->user_data);
    }
}

static void _on_app_control_cb(app_control_h app_control, void *user_data)
{
}

