/*
 * 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 "../../common/inc/ui-utils.h"
#include "../inc/image-common.h"
#include <Elementary.h>

#define IMAGE_REL_PATH_TEMPLATE "%s/%s"
#define IMAGES_REL_FOLDER "images"
#define SLIDER_INDICATOR_FORMAT "%1.0f"
#define IMAGE_MIN_SCALED_SIZE 16

static int _to_int(double value);

bool ic_load_image(Evas_Object *image, const char *file_name)
{
    RETVM_IF(NULL == image, false, "image is NULL");
    char buf[PATH_MAX] = {'\0'};
    snprintf(buf, sizeof(buf), IMAGE_REL_PATH_TEMPLATE, IMAGES_REL_FOLDER, file_name);

    bool result = (EINA_TRUE == elm_image_file_set(image, get_resource_path(buf), NULL));
    if (!result)
    {
        ERR("error with elm_image_file_set, image path %s", buf);
    }
    return result;
}

void ic_main_layout_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
    free(data);
}

Evas_Object *ic_create_grid(Evas_Object *parent, double rel_max_width, double rel_max_height)
{
    Evas_Object *grid = elm_grid_add(parent);
    if (grid)
    {
        elm_grid_size_set(grid, rel_max_width, rel_max_height);
        elm_object_part_content_set(parent, "elm.swallow.content" , grid);
        evas_object_show(grid);
    }
    return grid;
}

Evas_Object *ic_create_image(Evas_Object *parent, const char *file_name)
{
    Evas_Object *image = elm_image_add(parent);
    if (image)
    {
        if (!ic_load_image(image, file_name))
        {
            ERR("error with elm_image_file_set, image name %s", file_name);
            evas_object_del(image);
            return NULL;
        }
        elm_image_no_scale_set(image, EINA_FALSE);
        elm_image_resizable_set(image, EINA_TRUE, EINA_TRUE);
        elm_image_smooth_set(image, EINA_FALSE);
        elm_image_aspect_fixed_set(image, EINA_TRUE);
        elm_image_editable_set(image, EINA_FALSE);
        evas_object_show(image);
    }
    return image;
}

Evas_Object *ic_create_slider(Evas_Object *parent, double slider_min, double slider_max)
{
    Evas_Object *slider = elm_slider_add(parent);
    if (slider)
    {
        elm_slider_min_max_set(slider, slider_min, slider_max);
        elm_slider_step_set(slider, 1.0);
        elm_slider_indicator_format_set(slider, SLIDER_INDICATOR_FORMAT);
        elm_slider_indicator_show_set(slider, EINA_TRUE);
        evas_object_show(slider);
    }
    return slider;
}

Evas_Object *ic_create_label(Evas_Object *parent)
{
    Evas_Object *label = elm_label_add(parent);
    if (label)
    {
        evas_object_show(label);
    }
    return label;
}

Evas_Object *ic_create_scroller(Evas_Object *parent)
{
    Evas_Object *scroller = elm_scroller_add(parent);
    if (scroller)
    {
        evas_object_show(scroller);
    }
    return scroller;
}

void ic_image_set_animated(Evas_Object *image)
{
    RETM_IF(NULL == image, "image is NULL");
    if (EINA_TRUE == elm_image_animated_available_get(image))
    {
        elm_image_animated_set(image, EINA_TRUE);
        elm_image_animated_play_set(image, EINA_TRUE);
    }
}

bool ic_calc_image_new_geometry(Evas_Object *evas_image, Evas_Object *evas_image_container,
                                    double scale, Evas_Coord_Rectangle *new_geometry)
{
    RETVM_IF(NULL == evas_image, false, "error: data passed as NULL");
    RETVM_IF(NULL == evas_image_container, false, "error: data passed as NULL");
    RETVM_IF(NULL == new_geometry, false, "error: data passed as NULL");
    int original_w, original_h;
    original_w = original_h = 0;
    evas_object_image_size_get(evas_image, &original_w, &original_h);

    new_geometry->w = _to_int(original_w * scale);
    new_geometry->h = _to_int(original_h * scale);
    if (new_geometry->w < IMAGE_MIN_SCALED_SIZE)
    {
        new_geometry->w = IMAGE_MIN_SCALED_SIZE;
    }
    if (new_geometry->h < IMAGE_MIN_SCALED_SIZE)
    {
        new_geometry->h = IMAGE_MIN_SCALED_SIZE;
    }

    new_geometry->x = - new_geometry->w / 2;
    new_geometry->y = - new_geometry->h / 2;
    Evas_Coord_Rectangle container_geometry = ic_get_geometry(evas_image_container);
    new_geometry->x += container_geometry.x + container_geometry.w/2;
    new_geometry->y += container_geometry.y + container_geometry.h/2;

    return true;
}

Evas_Coord_Rectangle ic_get_geometry(const Evas_Object* object)
{
    Evas_Coord_Rectangle geometry = {};
    evas_object_geometry_get(object, &(geometry.x), &(geometry.y),
                             &(geometry.w), &(geometry.h));
    return geometry;
}

static int _to_int(double value)
{
    return (int) value;
}
