# -*- coding: utf-8 -*-
import logging
from util import exec_remote_command_host
from base import BaseInstanceStep

LOG = logging.getLogger(__name__)


class Disk(BaseInstanceStep):
    OLD_DIRECTORY = '/data'
    NEW_DIRECTORY = '/new_data'

    @property
    def is_valid(self):
        return bool(self.instance.hostname.volumes.first())

    @property
    def has_active(self):
        return self.host.volumes.filter(is_active=True).exists()


class DiskCommand(Disk):

    @property
    def scripts(self):
        raise NotImplementedError

    def do(self):
        if not self.is_valid:
            return

        for message, script in self.scripts.items():
            output = {}
            return_code = exec_remote_command_host(self.host, script, output)
            if return_code != 0:
                raise EnvironmentError(
                    '{} - {}: {}'.format(message, return_code, output)
                )

    def undo(self):
        # TODO
        pass


class CopyDataBetweenExports(DiskCommand):

    def __unicode__(self):
        return "Coping data {} -> {}...".format(
            self.OLD_DIRECTORY, self.NEW_DIRECTORY
        )

    @property
    def scripts(self):
        message = 'Could not copy data {} -> {}'.format(
            self.OLD_DIRECTORY, self.NEW_DIRECTORY)
        script = "rsync -arm --exclude='{0}/.snapshot' {0}/* {1}".format(
            self.OLD_DIRECTORY, self.NEW_DIRECTORY
        )
        return {message: script}


class CopyDataBetweenExportsMigration(CopyDataBetweenExports):
    NEW_DIRECTORY = '{}/data'.format(CopyDataBetweenExports.OLD_DIRECTORY)
    OLD_DIRECTORY = '{}/data'.format(CopyDataBetweenExports.NEW_DIRECTORY)

    @property
    def is_valid(self):
        is_valid = super(CopyDataBetweenExportsMigration, self).is_valid
        if not is_valid:
            return False

        if not self.infra.plan.has_persistence:
            return False

        return True

    @property
    def host(self):
        host = super(CopyDataBetweenExportsMigration, self).host
        return host.future_host


class CleanData(DiskCommand):
    def __unicode__(self):
        return "Removing data from slaves..."

    @property
    def is_valid(self):
        return self.restore.is_slave(self.instance)

    @property
    def directory(self):
        return '{}/data/*'.format(self.OLD_DIRECTORY)

    @property
    def scripts(self):
        message = 'Could not remove data from {}'.format(self.OLD_DIRECTORY)
        script = 'rm -rf {}'.format(self.directory)
        return {message: script}


class CleanDataArbiter(CleanData):

    def __unicode__(self):
        return "Removing data from arbiter..."

    @property
    def is_valid(self):
        return self.instance.instance_type == self.instance.MONGODB_ARBITER


class RemoveDeprecatedFiles(DiskCommand):

    def __unicode__(self):
        return "Removing deprecated files..."

    @property
    def scripts(self):
        driver = self.infra.get_driver()
        return {'Remove Deprecated': driver.remove_deprectaed_files()}


class ChangeSnapshotOwner(Disk):
    def __unicode__(self):
        return "Change snapshots owner..."

    @property
    def can_run(self):
        if self.host_migrate.database_migrate:
            return False
        return super(ChangeSnapshotOwner, self).can_run

    def do(self):
        for volume in self.instance.hostname.volumes.all():
            volume.is_active = False
            volume.host = self.host
            volume.save()

    def undo(self):
        volume = None
        for volume in self.host.volumes.filter(is_active=False):
            volume.host = self.instance.hostname
            volume.save()

        if volume:
            volume.is_active = True
            volume.save()

@@ -2127,32 +2127,122 @@
 nicode__(self):%0A
+        if not self.is_valid:%0A            return %22Skipped because the instance is master%22%0A
         return %22
@@ -2265,17 +2265,16 @@
 om slave
-s
 ...%22%0A%0A  
@@ -2622,16 +2622,16 @@
 ectory)%0A
-
         
@@ -2653,24 +2653,356 @@
 : script%7D%0A%0A%0A
+class CleanDataRecreateSlave(CleanData):%0A    @property%0A    def is_valid(self):%0A        return self.instance.is_slave%0A%0A%0Aclass CleanReplRecreateSlave(CleanData):%0A    @property%0A    def is_valid(self):%0A        return self.instance.is_slave%0A%0A    @property%0A    def directory(self):%0A        return '%7B%7D/repl/*'.format(self.OLD_DIRECTORY)%0A%0A%0A
 class CleanD

Create CleanReplRecreateSlave and CleanDataRecreateSlave steps