class LoginFrame(wx.Frame):

    def __init__(self):
        super(LoginFrame, self).__init__()
        self.res = xrc.XmlResource('../view/login.xrc')
        self.user_controller = UserController()
        self.main_menu = None
        self.inicio_user = None
        self.frame = self.res.LoadFrame(None, 'LoginFrame')
        self.panel = xrc.XRCCTRL(self.frame, 'InicioPanel')
        self.username = xrc.XRCCTRL(self.frame, 'text_user')
        self.password = xrc.XRCCTRL(self.frame, 'text_pass')
        self.button = xrc.XRCCTRL(self.panel, 'sign_in')
        self.frame.Bind(wx.EVT_BUTTON, self.validate_user, self.button)
        self.open = xrc.XRCCTRL(self.panel, 'account')
        self.frame.Bind(wx.EVT_BUTTON, self.open_register, self.open)
        self.frame.Show()

    def open_register(self, evt):
        self.register = RegistroFrame()

    def validate_user(self, evt):
        username = self.username.GetValue()
        password = self.password.GetValue()

        if username and password:
            if self.user_controller.search_user(username, password):
                if username == '@gustavo15' and password == 'gustavo12345':
                    self.frame.Close()
                    self.main_menu = InicioFrame()

                else:
                    self.frame.Close()
                    user = self.user_controller.search_username(username)
                    if user is not False:
                        self.inicio_user = InicioUser(user=user, frame_father=self)

                    else:
                        wx.MessageBox('User is not valid', 'Error', wx.OK | wx.ICON_INFORMATION)

            else:
                wx.MessageBox('User does not exist', 'Error', wx.OK | wx.ICON_ERROR)

        else:
            wx.MessageBox('Fields not entered', 'Error', wx.OK | wx.ICON_ERROR)
class RegistroFrame(wx.Frame):
    def __init__(self, frame_father=None, id_user=None):
        super(RegistroFrame, self).__init__()
        self.res = xrc.XmlResource('../view/registro.xrc')
        self.user_controller = UserController()
        self.id_user = id_user
        self.frame = self.res.LoadFrame(None, 'RegistroFrame')
        self.panel = xrc.XRCCTRL(self.frame, 'InicioRegistro')
        self.first_name = xrc.XRCCTRL(self.panel, 'textCtrlFirstName')
        self.last_name = xrc.XRCCTRL(self.panel, 'textCtrlLastname')
        self.username = xrc.XRCCTRL(self.panel, 'textCtrlUsername')
        self.password = xrc.XRCCTRL(self.panel, 'textCtrlPassword')
        self.button_save = xrc.XRCCTRL(self.panel, 'wxID_OK')
        self.button_cancel = xrc.XRCCTRL(self.panel, 'wxID_CANCEL')

        self.frame.Bind(wx.EVT_BUTTON, self.close_frame, self.button_cancel)
        self.frame.Bind(wx.EVT_BUTTON, self.create_user, self.button_save)

        self.frame_father = frame_father

        if self.id_user is not None:
            self.load_data_user()

        self.frame.Show()

    def close_frame(self, evt):
        self.frame.Close()

    def create_user(self, evt):
        first_name = self.first_name.GetValue()
        last_name = self.last_name.GetValue()
        username = self.username.GetValue()
        password = self.password.GetValue()

        search_data = self.user_controller.search_username(username)

        if first_name and last_name and username and password:

            if self.id_user is not None:
                data = {
                    'first_name': first_name,
                    'last_name': last_name,
                    'username': username,
                    'password': password
                }
                self.user_controller.edit_user(self.id_user, data)
                self.load_data_user()

            else:
                if search_data is None:
                    self.user_controller.create_user(first_name, last_name,
                                                     username, password)
                    wx.MessageBox('The user has been created successfully',
                                  'Information', wx.OK | wx.ICON_INFORMATION)
                    self.clear_fields()

                else:
                    wx.MessageBox(
                        'The username already exist in the database.', 'Error',
                        wx.OK | wx.ICON_ERROR)

            if not self.frame_father is None:
                self.frame_father.load_data_listctrl_user()

        else:
            wx.MessageBox('Fields not entered', 'Error', wx.OK | wx.ICON_ERROR)

    def clear_fields(self):
        self.first_name.Clear()
        self.last_name.Clear()
        self.username.Clear()
        self.password.Clear()

    def load_data_user(self):
        user = self.user_controller.get_user(self.id_user)
        self.first_name.SetValue(user.first_name)
        self.last_name.SetValue(user.last_name)
        self.username.SetValue(user.username)
        self.password.SetValue(user.password)