/*
 * 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/window.h"

#include <Elementary.h>

#define WINDOW_PREFERED_ENGINE "opengl_x11"

static const int view_rotation_state_270 = 270;

window_obj *win_create()
{
    window_obj *obj = calloc(1, sizeof(window_obj));
    if(!obj)
    {
        return NULL;
    }

    elm_config_preferred_engine_set(WINDOW_PREFERED_ENGINE);

    obj->win = elm_win_add(NULL, "$(appName)", ELM_WIN_BASIC);
    elm_win_conformant_set(obj->win, EINA_TRUE);
    evas_object_show(obj->win);

    elm_win_indicator_mode_set(obj->win, ELM_WIN_INDICATOR_SHOW);
    obj->conform = elm_conformant_add(obj->win);
    evas_object_size_hint_weight_set(obj->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    elm_win_resize_object_add(obj->win, obj->conform);
    evas_object_show(obj->conform);

    obj->bg = elm_bg_add(obj->conform);
    evas_object_size_hint_weight_set(obj->bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
    evas_object_show(obj->bg);

    elm_object_part_content_set(obj->conform, "elm.swallow.bg", obj->bg);

    return obj;
}

void win_destroy(window_obj *obj)
{
    if(obj)
    {
        evas_object_del(obj->win);
        free(obj);
    }
}

void win_lower(window_obj *obj)
{
    if(obj)
    {
        elm_win_lower(obj->win);
    }
}

void win_set_rotation(window_obj *obj, int rotation)
{
    if(obj && obj->win)
    {
        elm_win_rotation_with_resize_set(obj->win, rotation);
    }
}

int win_get_rotation(window_obj *obj)
{
    int result = 0;
    if(obj && obj->win)
    {
        result = elm_win_rotation_get(obj->win);
    }
    return result;
}

void win_show_indicator(window_obj *obj, Eina_Bool indicator)
{
    if(obj && obj->win)
    {
        if(EINA_TRUE == indicator)
        {
            elm_win_indicator_mode_set(obj->win, ELM_WIN_INDICATOR_SHOW);
        }
        else
        {
            elm_win_indicator_mode_set(obj->win, ELM_WIN_INDICATOR_HIDE);
        }
    }
}

void win_set_layout(window_obj *obj, Evas_Object *layout)
{
    if(!obj || !layout)
    {
        return;
    }
    elm_object_part_content_set(obj->conform, "elm.swallow.content", layout);
    obj->layout = layout;
}

static void _set_default_style(window_obj *obj)
{
    if(obj->win)
    {
        elm_win_indicator_mode_set(obj->win, ELM_WIN_INDICATOR_SHOW);
        elm_win_rotation_with_resize_set(obj->win, 0);
    }
}

static void _set_video_recorder_player_style(window_obj *obj)
{
    if(obj->win)
    {
        elm_win_indicator_mode_set(obj->win, ELM_WIN_INDICATOR_HIDE);
        elm_win_rotation_with_resize_set(obj->win, view_rotation_state_270);
    }
}

void win_set_view_style(window_obj *obj, window_view_style style)
{
    if(obj)
    {
        obj->style = style;
        switch(obj->style)
        {
            case STYLE_DEFAULT_WINDOW :
                _set_default_style(obj);
                break;
            case STYLE_VIDEO_RECORDER_PLAYER :
                _set_video_recorder_player_style(obj);
                break;
            default:
                break;
        }
    }
}

window_view_style win_get_view_style(const window_obj *obj)
{
    window_view_style result = STYLE_NONE;
    if(obj)
    {
        result = obj->style;
    }
    return result;
}

Evas_Object *win_get_host_layout(const window_obj *win)
{
    if(!win)
    {
        return NULL;
    }
    return win->conform;
}
