'''
Created on Apr 14, 2018

@author: johnrabsonjr
'''

from my.classes.dollhouse import OurDollhouseSuperclass
from my.miscellany import dict_to_namedtuple


class FrontendDollhouse(OurDollhouseSuperclass):
    def __init__(self):
        super().__init__()

    @property
    def is_mounted(
            self
    ):  # is the dollhouse mounted (and therefore almost certainly unlocked)?
        from my.frontend import Frontend
        return dict_to_namedtuple(
            Frontend.talk_to_backend(
                'backendtask__get_dollhouse_status')).mounted

    @property
    def is_open(self):  # is the dollhouse open (unlocked AND mounted)?
        from my.frontend import Frontend
        return dict_to_namedtuple(
            Frontend.talk_to_backend('backendtask__get_dollhouse_status')).open

    @property
    def is_unlocked(
            self
    ):  # is the dollhouse unlocked? (FYI, it might/might not be mounted)
        from my.frontend import Frontend
        return dict_to_namedtuple(
            Frontend.talk_to_backend(
                'backendtask__get_dollhouse_status')).unlocked

    @property
    def disk_usage(self):  # get the percentage of dollhouse disk space in use
        from my.frontend import Frontend
        return dict_to_namedtuple(
            Frontend.talk_to_backend(
                'backendtask__get_dollhouse_status')).diskused

    def open(self, pw,
             wipe=False):  # (wipe? &)mount dollhouse w/ supplied password
        from my.frontend import Frontend
        dct = {'mypass': pw, 'mypass2': pw if wipe else None}
        return dict_to_namedtuple(
            Frontend.talk_to_backend('backendtask__open_dollhouse', dct))

    def close(self):  # close dollhouse (unmount & lock it)
        from my.frontend import Frontend
        return dict_to_namedtuple(
            Frontend.talk_to_backend('backendtask__close_dollhouse'))

    def unlock(self, pw, wipe=False):  # unlock the crypto device
        from my.frontend import Frontend
        return dict_to_namedtuple(
            Frontend.talk_to_backend('backendtask__unlock_dollhouse', {
                'pw': pw,
                'wipe': wipe
            }))

    def mount(
            self
    ):  # mount the /home unionfs-mounted partition and the crypto device
        from my.frontend import Frontend
        return Frontend.talk_to_backend(
            'backendtask__mount_the_unlocked_dollhouse')

    def unmount(
            self
    ):  # unmount the /home unionfs-mounted partition and the crypto device
        from my.frontend import Frontend
        return Frontend.talk_to_backend(
            'backendtask__unmount_the_unlocked_dollhouse')

    def lock(self):  # lock crypto device
        from my.frontend import Frontend
        return Frontend.talk_to_backend('backendtask__lock_dollhouse')
