/*
 * 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 "config.h"
#include "../../image/inc/capture-image-viewer.h"
#include "../../common/inc/main-app.h"
#include "../../common/inc/logger.h"

#include <Elementary.h>
#include <string.h>
#include <libgen.h>

#define MAX_TEXT_LEN 512
#define TEXT_PADDING 115
#define STR_TITLE "Image viewer"
#define STR_FILE_NAME "File name"
#define STR_FILE_SIZE "File size"
#define STR_DIM "Dimension"
#define STR_KB "KB"

struct _capture_image_viewer
{
    app_data *app;
    Elm_Object_Item *frame_item;
    Evas_Object *navi;
    Evas_Object *layout;
    const char *image_path;
    capture_image_viewer_del_cb del_cb;
    const void *user_data;
};

static void _destroy(capture_image_viewer *view);
static void _push(capture_image_viewer *view);
static Evas_Object *_main_layout_add(capture_image_viewer *view, Evas_Object *parent);
static Evas_Object *_image_add(Evas_Object *parent, const char *path);
static Evas_Object *_box_add(Evas_Object *parent);
static Evas_Object *_text_item_add(Evas_Object *box, const char *text);
static long long _get_file_size(const char *file_path);

static void _on_main_layout_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);

capture_image_viewer *capture_image_viewer_add(struct _app_data *app, Evas_Object *navi,
        const char *image_path, capture_image_viewer_del_cb del_cb, const void *user_data)
{
    RETVM_IF(!image_path, NULL, "image_path is null");

    capture_image_viewer *view = calloc(1, sizeof(capture_image_viewer));

    if(view)
    {
        view->navi = navi;
        view->app = app;
        view->image_path = image_path;
        view->del_cb = del_cb;
        view->user_data = user_data;
        view->layout = _main_layout_add(view, navi);

        if(view->layout)
        {
            evas_object_event_callback_add(view->layout, EVAS_CALLBACK_DEL, _on_main_layout_del_cb, view);
            _push(view);
        }
        else
        {
            _destroy(view);
            return NULL;
        }
    }

    return view;
}

static void _destroy(capture_image_viewer *view)
{
    free(view);
}

static void _push(capture_image_viewer *view)
{
    view->frame_item = elm_naviframe_item_push(view->navi, STR_TITLE, NULL, NULL, view->layout, NULL);
}

static Evas_Object *_main_layout_add(capture_image_viewer *view, Evas_Object *parent)
{
    Evas_Object *layout = elm_layout_add(parent);
    view->layout = layout;

    elm_layout_file_set(layout, CAPTUREIMGVIEWEREEDJ, "capture_image_viewer");
    evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
    evas_object_show(layout);

    Evas_Object *image = _image_add(layout, view->image_path);
    Evas_Object *box = _box_add(layout);

    elm_object_part_content_set(layout, "image", image);
    elm_object_part_content_set(layout, "text", box);

    char buf[MAX_TEXT_LEN] = {'\0'};

    snprintf(buf, MAX_TEXT_LEN, "%s: %s", STR_FILE_NAME, basename((char*)view->image_path));
    _text_item_add(box, buf);

    long long file_size = _get_file_size(view->image_path) / 1024;
    snprintf(buf, MAX_TEXT_LEN, "%s: %lld %s", STR_FILE_SIZE, file_size, STR_KB);
    _text_item_add(box, buf);

    int w = 0;
    int h = 0;
    elm_image_object_size_get(image, &w, &h);
    snprintf(buf, MAX_TEXT_LEN, "%s: %dx%d", STR_DIM, w, h);
    _text_item_add(box, buf);

    return layout;
}

static Evas_Object *_image_add(Evas_Object *parent, const char *path)
{
    Evas_Object *image = elm_image_add(parent);
    if (image)
    {
        elm_image_file_set(image, path, NULL);
        elm_image_no_scale_set(image, EINA_FALSE);
        elm_image_resizable_set(image, EINA_TRUE, EINA_TRUE);
        elm_image_smooth_set(image, EINA_TRUE);
        elm_image_aspect_fixed_set(image, EINA_TRUE);
        elm_image_editable_set(image, EINA_FALSE);
        evas_object_show(image);
    }
    return image;
}

static Evas_Object *_box_add(Evas_Object *parent)
{
    Evas_Object *box = elm_box_add(parent);
    evas_object_show(box);
    elm_box_homogeneous_set(box, EINA_TRUE);
    elm_box_horizontal_set(box, EINA_FALSE);
    return box;
}

static Evas_Object *_text_item_add(Evas_Object *box, const char *text)
{
    Evas_Object *label = elm_label_add(box);
    evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(label, 0.0, EVAS_HINT_FILL);
    evas_object_show(label);

    char buf[MAX_TEXT_LEN] = {'\0'};
    snprintf(buf, MAX_TEXT_LEN, "%s", text);

    elm_object_text_set(label, buf);

    elm_box_pack_end(box, label);

    return label;
}

static void _on_main_layout_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
    capture_image_viewer *view = (capture_image_viewer *)data;
    if(view)
    {
        if(view->del_cb)
        {
            view->del_cb((void*)view->user_data);
        }
        _destroy(view);
    }
}

static long long _get_file_size(const char *file_path)
{
    FILE *f = fopen(file_path, "r");
    long long size = 0;

    if(f)
    {
        fseek(f, 0, SEEK_END);
        size = ftell(f);
        fclose(f);
    }

    return size;
}
