/*
 * 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/slider-popup.h"
#include "../inc/logger.h"
#include "../inc/ui-utils.h"
#include <efl_extension.h>

#define LABEL_LEN 64

struct _slider_popup
{
    app_data *app;
    Evas_Object *parent;
    Evas_Object *popup;
    Evas_Object *slider;
    Evas_Object *label;
    char *label_text;
    int value;
    slider_popup_changed_cb changed_cb;
    slider_popup_closed_cb closed_cb;
    void *user_data;
    int min;
    int max;
    bool deleting;
};

static const float size_ratio = 0.25;
static const char *str_ok = "Ok";
static const char *label_template = "%s : %.0f";

static Evas_Object *_popup_add(slider_popup *obj, Evas_Object *parent, Elm_Popup_Orient orient, const char *title);
static Evas_Object *_content_layout_add(slider_popup *obj, Evas_Object *parent);
static void _on_main_layout_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
static void _slider_changed(void *data, Evas_Object *obj, void *event_info);
static void _on_ok_btn_cb(void *data, Evas_Object *obj, void *event_info);
static void _on_hw_back_btn_cb(void *data, Evas_Object *obj, void *event_info);
static void _close(slider_popup *popup, bool result);

slider_popup *slider_popup_add(app_data *app, Evas_Object *parent, Elm_Popup_Orient orient, const char *title)
{
    slider_popup *obj = calloc(1, sizeof(slider_popup));
    if(obj)
    {
        obj->parent = parent;
        obj->app = app;
        obj->popup = _popup_add(obj, parent, orient, title);
    }

    return obj;
}

void slider_popup_del(slider_popup *obj)
{
    if(obj && !obj->deleting)
    {
        free(obj->label_text);

        obj->deleting = true;
        evas_object_event_callback_del(obj->popup, EVAS_CALLBACK_DEL, _on_main_layout_del_cb);
        evas_object_del(obj->popup);
        free(obj);
    }
}

void slider_popup_label_set(slider_popup *obj, const char *label)
{
    if(obj && label)
    {
        free(obj->label_text);
        obj->label_text = strdup(label);
    }
}

void slider_popup_min_max_set(slider_popup *obj, int min, int max)
{
    if(obj && obj->slider)
    {
        elm_slider_min_max_set(obj->slider, (double)min, (double)(max + 0.5));
    }
}

void slider_popup_value_set(slider_popup *obj, int value)
{
    if(obj && obj->slider)
    {
        obj->value = value;
    }
}

static void _update_value(slider_popup *obj)
{
    if(obj->slider)
    {
        elm_slider_value_set(obj->slider, (double)obj->value);
    }
    if(obj->label)
    {
        char buf[LABEL_LEN] = {'\0'};
        snprintf(buf, LABEL_LEN, label_template, obj->label_text, (double)obj->value);
        elm_object_text_set(obj->label, buf);
    }
}

void slider_popup_callback_set(slider_popup *obj, slider_popup_changed_cb changed_cb,
        slider_popup_closed_cb closed_cb, const void *user_data)
{
    if(obj)
    {
        obj->changed_cb = changed_cb;
        obj->closed_cb = closed_cb;
        obj->user_data = (void*)user_data;
    }
}

void slider_popup_show(slider_popup *obj)
{
    if(obj)
    {
        _update_value(obj);
    }
}

int slider_popup_value_get(const slider_popup *obj)
{
    return obj ? obj->value : 0;
}

static Evas_Object *_popup_add(slider_popup *obj, Evas_Object *parent, Elm_Popup_Orient orient, const char *title)
{
    Evas_Object *popup = elm_popup_add(parent);
    elm_popup_orient_set(popup, orient);
    eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, _on_hw_back_btn_cb, obj);

    evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

    elm_object_part_text_set(popup, "title,text", title ? title : "");
    evas_object_event_callback_add(popup, EVAS_CALLBACK_DEL, _on_main_layout_del_cb, obj);

    Evas_Object * button = elm_button_add(popup);
    elm_object_text_set(button, str_ok);
    evas_object_smart_callback_add(button, "clicked", _on_ok_btn_cb, obj);
    elm_object_part_content_set(popup, "button1", button);

    Evas_Object *content = _content_layout_add(obj, popup);
    elm_object_content_set(popup, content);

    evas_object_show(popup);

    return popup;
}

static Evas_Object *_content_layout_add(slider_popup *obj, Evas_Object *parent)
{
    Evas_Object *box = elm_box_add(parent);
    evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
    evas_object_show(box);

    int h = 0;
    evas_object_geometry_get(obj->parent, NULL, NULL, NULL, &h);

    evas_object_size_hint_min_set(box, 0, h * size_ratio);

    obj->label = elm_label_add(box);
    evas_object_size_hint_weight_set(obj->label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(obj->label, 0.1, 0.5);
    evas_object_show(obj->label);
    elm_box_pack_start(box, obj->label);

    obj->slider = elm_slider_add(box);
    evas_object_size_hint_weight_set(obj->slider, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_size_hint_align_set(obj->slider, EVAS_HINT_FILL, EVAS_HINT_FILL);
    evas_object_smart_callback_add(obj->slider, "changed", _slider_changed, obj);
    evas_object_show(obj->slider);
    elm_box_pack_end(box, obj->slider);


    return box;
}

static void _on_main_layout_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
    slider_popup *popup = data;
    if(popup)
    {
        slider_popup_del(popup);
    }
}

static void _on_ok_btn_cb(void *data, Evas_Object *obj, void *event_info)
{
    slider_popup *popup = data;
    if(popup)
    {
        _close(popup, true);
    }
}

static void _slider_changed(void *data, Evas_Object *obj, void *event_info)
{
    slider_popup *popup = data;
    if(popup)
    {
        int value = (int)elm_slider_value_get(obj);
        if(popup->value != value)
        {
            popup->value = value;
            _update_value(popup);
            if(popup->changed_cb)
            {
                popup->changed_cb(popup, popup->user_data);
            }
        }
    }
}

static void _close(slider_popup *popup, bool result)
{
    if(popup->closed_cb)
    {
        popup->closed_cb(popup, result, popup->user_data);
    }
}

static void _on_hw_back_btn_cb(void *data, Evas_Object *obj, void *event_info)
{
    slider_popup *popup = data;
    if(popup)
    {
        _close(popup, false);
    }
}


