# -*- Mode: Python; coding: utf-8; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
#
# Function view_period
# Copyright (C) 2017 NovaDragoN <xmmendezy@gmail.com>

import os
import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import Data_process
from Data_process import data_view_registrer
from Core import view_login_super_user


def period(lista):
    applicationwindow, id_period, model_period, data_login = lista

    gui_period = Gtk.Builder()
    gui_period.add_from_file('{0}/Gtk-ui/view_period.ui'.format(os.getcwd()))

    # Tomar objetos de la interfaz de usuario
    dialog_period = gui_period.get_object('dialog_period')
    dialog_period.set_transient_for(applicationwindow)
    name_period = gui_period.get_object('name_period')
    button_date_begin = gui_period.get_object('button_date_begin')
    revealer_date_begin = gui_period.get_object('revealer_date_begin')
    calendar_date_begin = gui_period.get_object('calendar_date_begin')
    button_date_end = gui_period.get_object('button_date_end')
    revealer_date_end = gui_period.get_object('revealer_date_end')
    calendar_date_end = gui_period.get_object('calendar_date_end')
    label_info = gui_period.get_object('label_info')
    button_update = gui_period.get_object('button_update')
    button_delete = gui_period.get_object('button_delete')
    button_cancel = gui_period.get_object('button_cancel')

    # Preparar calendario
    months = ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre',
              'noviembre', 'diciembre']

    # Llenar objetos de la interfaz de usuario
    data_period = \
        Data_process.return_data_sql('Name_Period, Date_begin, Date_end', 'Periods',
                                     'id_period = "{0}"'.format(id_period),
                                     [], [1, 2])[0]
    name_period.set_text(data_period[0])
    calendar_date_begin.select_day(data_period[1][2])
    calendar_date_begin.select_month(data_period[1][1], data_period[1][0])
    button_date_begin.set_label(
        '{0} / {1} / {2}'.format(str(data_period[1][2]), months[data_period[1][1]].capitalize(),
                                 str(data_period[1][0])))
    calendar_date_end.select_day(data_period[2][2])
    calendar_date_end.select_month(data_period[2][1], data_period[2][0])
    button_date_end.set_label(
        '{0} / {1} / {2}'.format(str(data_period[2][2]), months[data_period[2][1]].capitalize(),
                                 str(data_period[2][0])))
    data = [id_period, name_period, calendar_date_begin, calendar_date_end]

    # Conectar señales a los objetos de la interfaz de usuario
    button_date_begin.connect('clicked', lambda w: revealer_date_begin.set_reveal_child(not revealer_date_begin.get_reveal_child()))
    calendar_date_begin.connect('day-selected', data_view_registrer.day_registrer, months,
                                button_date_begin)
    button_date_end.connect('clicked', lambda w: revealer_date_end.set_reveal_child(not revealer_date_end.get_reveal_child()))
    calendar_date_end.connect('day-selected', data_view_registrer.day_registrer, months, button_date_end)
    button_update.connect('activate', view_login_super_user.is_login_super_user, Data_process.update_period,
                          dialog_period, data, model_period, label_info)
    button_update.connect('clicked', view_login_super_user.is_login_super_user, Data_process.update_period,
                          dialog_period, data, model_period, label_info)
    button_delete.connect('activate', view_login_super_user.is_login_super_user, Data_process.delete_period,
                          dialog_period, [id_period, name_period.get_text()], model_period)
    button_delete.connect('clicked', view_login_super_user.is_login_super_user, Data_process.delete_period,
                          dialog_period, [id_period, name_period.get_text()], model_period)
    button_cancel.connect('clicked', lambda w: dialog_period.hide())

    # Presentar la ventana
    dialog_period.run()
    dialog_period.hide()
