"""
This module provides the PasswordSetting class.

"""

###############################################################################
# Import
###############################################################################

from gui import Entry
from guardian import (
    check_type,
    check_type_and_truth,
    check_type_and_value,
    is_python_3,
    )
from settings.setting.base_setting import BaseSetting

###############################################################################
# Python version compatibility
###############################################################################

if is_python_3():  # pragma: no cover
    basestring = str

###############################################################################
# Public
###############################################################################


class PasswordSetting(BaseSetting):
    """ Setting of type string.

        Parameters
        ----------
        name : str
            The settings name.
        value : str
            The settings value.
        group : str, optional
            The settings group. Default is 'general'.

    """

    def __init__(self, name, value, group='general'):
        n = '{}.__init__'.format(self.get_class_name())
        check_type_and_truth(n, basestring, name=name)
        check_type(n, basestring, value=value)

        enc_value = Entry._encrypt_string('PasswordSetting', value)
        super(PasswordSetting, self).__init__(name, enc_value, group)

    def add_to_gui(self, master, tab, line, name_size, obj_size):
        """ Adds the setting on a ttk frame.

            Parameters
            ----------
            master : Tkinter element
                The gui to add. Can be None in case of the main gui.
            tab : settings.SettingsTab
                Tab to add setting.
            line : int
                Next free grid line.
            name_size : int
                The size of the name-label.
            obj_size : int
                The absolute object size.

            Returns
            -------
            int
                Next free grid line.

        """
        n = '{}.add_to_gui'.format(self.get_class_name())
        check_type_and_value(n, int,
                             min_value=0, max_value=None,
                             line=line)
        check_type_and_value(n, int,
                             min_value=1, max_value=None,
                             name_size=name_size, obj_size=obj_size)

        enc_value = self.get_value()
        value = Entry._decrypt_string('PasswordSetting', enc_value)
        self.gui_repr = Entry(master, value)
        self.gui_repr.set_length(obj_size)
        self.gui_repr.cover_value()
        self.gui_repr.set_sticky('ew')

        tab.add(self.get_label(master, name_size), (line, 0))
        tab.add(self.gui_repr, (line, 1))

        return line + 1

    def load_from_gui(self):
        """ Transfers the value from GUI back into the settings value."""
        enc_value = Entry._encrypt_string('PasswordSetting',
                                          self.gui_repr.get_value())
        self.set_value(enc_value)

    def reset_gui_value(self):
        """ Replaces the GUI value with the settings value."""
        enc_value = self.get_value()
        value = Entry._decrypt_string('PasswordSetting', enc_value)
        self.gui_repr.set_value(value)

#    def add_dependency(self, dependency):
#        """ Add a new dependency.
#
#            Parameters
#            ----------
#            dependency : Subclass of Dependency
#                A settings dependency.
#        """
#        class DependencyNotImplementedError(Exception):
#            pass
#        raise DependencyNotImplementedError(self.get_class_name())
