from .UpdatePfSenseConfiguration import UpdatePfSenseConfiguration
from ..Firewall.ConfigurePfSense import ConfigurePfSense
from ..Firewall.SetFirewallPassword import SetFirewallPassword
from ..General.EnsureSshKeysExist import EnsureSshKeysExist
from ..General.WriteRootSecret import WriteRootSecret
from ..General.WriteSecretFile import WriteSecretFile
from ..Server.PopulateDomadminSecret import PopulateDomadminSecret
from ..Server.PopulateFwradiusSecret import PopulateFwradiusSecret
from ..Server.PopulateLdapBase import PopulateLdapBase
from ..Server.PopulateLdapsucheSecret import PopulateLdapsucheSecret
from ..Server.PopulateRootSecret import PopulateRootSecret
from ..Server.SetLdapPassword import SetLdapPassword
from ...Machine.PaedMLMachineCollection import MACHINE_COLLECTION, LMZ_COMMON_CREDENTIALS
from ..General.SetPamPassword import SetPamPassword
from ...Logging.Logger import Severity, log
from ..General.CheckForSshSession import CheckForSSHSession
from ..General.TestCustomerAccess import TestCustomerAccess
from ..General.TestInternetConnection import TestInternetConnection
from ..General.TestConnection import TestConnection
from ..Playbooks.Playbook import Playbook


class ChangeRootPassword(Playbook):
    def start(self):
        if self.credentials.mli is None or self.credentials.mli_password is None:
            self.credentials.mli = self.ui.get_input("Please enter your MLI:")
            self.credentials.mli_password = self.ui.get_password("Please enter your MLI password:")

        if not self.is_sub_playbook:
            self.run_steps(TestInternetConnection(),
                       CheckForSSHSession())

        self.run_steps(TestCustomerAccess(),
                   TestConnection(MACHINE_COLLECTION.firewall, 22),
                   TestConnection(MACHINE_COLLECTION.backup, 22),
                   PopulateRootSecret()
                   )

        if not MACHINE_COLLECTION.initialized:
            if self.credentials.root is not None:
                log("Found existing credentials.", Severity.DEBUG)
                LMZ_COMMON_CREDENTIALS.set_password(self.credentials.root)

            log("Initializing machine connections...", Severity.INFO)
            MACHINE_COLLECTION.initialize()

        password = self.ui.get_password("Please enter the desired root password:", verify=True)
        while len(password) < 8:
            self.ui.print("Password is too short. Please enter at least 8 characters.")
            password = self.ui.get_password("Please enter the desired root password:", verify=True)
        self.credentials.root = password
        self.credentials.domadmin = password

        self.run_steps(SetPamPassword(MACHINE_COLLECTION.server, "root", password, "sha512crypt"),
                       WriteRootSecret(MACHINE_COLLECTION.server),
                       SetPamPassword(MACHINE_COLLECTION.backup, "root", password, "sha512crypt"),
                       WriteRootSecret(MACHINE_COLLECTION.backup),
                       SetFirewallPassword("root", self.credentials.root),
                       PopulateLdapsucheSecret(),
                       PopulateFwradiusSecret(),
                       PopulateDomadminSecret(),
                       WriteSecretFile(MACHINE_COLLECTION.server, "domadmin", self.credentials.domadmin),
                       PopulateLdapBase(),
                       SetLdapPassword(f'uid=Administrator,cn=users,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', password),
                       SetLdapPassword(f'uid=netzwerkberater,cn=admins,cn=users,ou=schule,{LMZ_COMMON_CREDENTIALS.get_ldap_base()}', password)
                       )

        if not self.is_sub_playbook:
            self.run_sub_playbook(UpdatePfSenseConfiguration)