"""Subclass of NewTask, which is generated by wxFormBuilder."""

import wx

from beatle.lib import wxx
from beatle.app import resources as rc
from beatle.model import tasks as tasks
from beatle.activity.tasks.ui import ui


# Implementing NewTask
class TaskDialog(ui.NewTask):
    """This dialog allows to configure the task properties.
    """
    @wxx.SetInfo(__doc__)
    def __init__(self, parent, container):
        """Initialize dialog"""
        self._container = container
        self.origin = None
        super(TaskDialog, self).__init__(parent)
        icon = wx.EmptyIcon()
        icon.CopyFromBitmap(rc.GetBitmap("glass_clock"))
        self.SetIcon(icon)
        self.m_staticText6.SetLabelText(wx.DateTime.Now().Format('%Y-%m-%d %H:%M:%S'))
        self.m_staticText7.SetLabelText('--/--/---- --:--:--')
        self.m_staticText72.SetLabelText('--/--/---- --:--:--')
        # The kind of parent determines the kind of task
        if type(self._container) is tasks.PendingTasks:
            self.m_choice1.SetSelection(0)
        elif type(self._container) is tasks.CurrentTasks:
            self.m_choice1.SetSelection(1)
        elif type(self._container) is tasks.FinishedTasks:
            self.m_choice1.SetSelection(2)
        self.m_choice1.Enable(False)

    def Validate(self):
        """Validate the dialog and returns ok or not ok"""
        # get name
        self._name = self.m_textCtrl2.GetValue().strip()
        if len(self._name) == 0:
            wx.MessageBox("Task Short description must be non empty", "Error",
                wx.OK | wx.CENTER | wx.ICON_ERROR, self)
            return False
        # get status
        i = self.m_choice1.GetSelection()
        self._status = self.m_choice1.GetString(i)
        # get priority
        i = self.m_choice3.GetSelection()
        self._priority = self.m_choice3.GetString(i)
        # get type
        i = self.m_choice2.GetSelection()
        self._taskType = self.m_choice2.GetString(i)
        # get note
        self._note = self.m_richText3.GetValue()
        # get dates
        self._dateCreated = self.m_staticText6.GetLabelText()
        self._dateBegin = self.m_staticText7.GetLabelText()
        self._dateEnd = self.m_staticText72.GetLabelText()
        return True

    def CopyAttributes(self, task):
        """Transfer attributes to task"""
        task._name = self._name
        task.note = self._note
        task._status = self._status
        task._priority = self._priority
        task._taskType = self._taskType
        task._dateCreated = self._dateCreated
        task._dateBegin = self._dateBegin
        task._dateEnd = self._dateEnd

    def SetAttributes(self, task):
        """Transfer attributes from existing task"""
        self._container = task.parent
        self._name = task._name
        self._note = task._note
        self._status = task._status
        self._priority = task._priority
        self._taskType = task._taskType
        self._dateCreated = task._dateCreated
        self._dateBegin = task._dateBegin
        self._dateEnd = task._dateEnd
        #
        i = self.m_choice1.FindString(self._status)
        if i != wx.NOT_FOUND:
            self.m_choice1.SetSelection(i)
        #
        i = self.m_choice3.FindString(self._priority)
        if i != wx.NOT_FOUND:
            self.m_choice3.SetSelection(i)
        #
        i = self.m_choice2.FindString(self._taskType)
        if i != wx.NOT_FOUND:
            self.m_choice2.SetSelection(i)
        #
        self.m_textCtrl2.SetValue(self._name)
        self.m_richText3.SetValue(self._note)
        self.m_staticText6.SetLabelText(self._dateCreated)
        self.m_staticText7.SetLabelText(self._dateBegin)
        self.m_staticText72.SetLabelText(self._dateEnd)
        self.SetTitle("Edit task")

    def get_kwargs(self):
        """return arguments suitable for object instance"""
        return {'parent': self._container, 'name': self._name,
            'note': self._note, 'status': self._status,
            'priority': self._priority, 'type': self._taskType,
            'dateCreated': self._dateCreated, 'dateBegin': self._dateBegin,
            'dateEnd': self._dateEnd,
            }

    # Handlers for NewTask events.
    def OnOK(self, event):
        """On Ok"""
        if self.Validate():
            self.EndModal(wx.ID_OK)

