from wizard_builder.views import WizardView
from wizard_builder.view_helpers import StorageHelper, StepsHelper

from django.core.urlresolvers import reverse

from .views import ReportAccessView, SecretKeyStorageHelper


class ReportStepsHelper(StepsHelper):

    def url(self, step):
        return reverse(
            self.view.request.resolver_match.view_name,
            kwargs={
                'step': step,
                'uuid': self.view.report.uuid,
            },
        )


class EncryptedStorageHelper(
    SecretKeyStorageHelper,
    StorageHelper,
):

    @property
    def form_data(self):
        return self.decrypt(super().form_data)

    @property
    def post_data(self):
        return self.encrypt(super().post_data)

    def encrypt(self, data):
        return data  # TODO

    def decrypt(self, data):
        return data  # TODO


class EncryptedWizardView(
    ReportAccessView,
    WizardView
):
    template_name = WizardView.template_name
    storage_helper = EncryptedStorageHelper
    steps_helper = ReportStepsHelper

    def form_valid(self, form):
        if self.storage.secret_key:
            self._save_report()
        return super().form_valid(form)

    def _save_report(self):
        self.report.encrypt_report(
            self.storage.form_data,
            self.storage.secret_key,
        )

from wizard_builder.views import WizardView
from wizard_builder.view_helpers import StorageHelper, StepsHelper

from django.core.urlresolvers import reverse

from .views import ReportAccessView, SecretKeyStorageHelper


class ReportStepsHelper(StepsHelper):

    def url(self, step):
        return reverse(
            self.view.request.resolver_match.view_name,
            kwargs={
                'step': step,
                'uuid': self.view.report.uuid,
            },
        )


class EncryptedStorageHelper(
    SecretKeyStorageHelper,
    StorageHelper,
):

    @property
    def form_data(self):
        return self.decrypt(super().form_data)

    @property
    def post_data(self):
        return self.encrypt(super().post_data)

    def encrypt(self, data):
        return data  # TODO

    def decrypt(self, data):
        return data  # TODO


class EncryptedWizardView(
    ReportAccessView,
    WizardView
):
    template_name = WizardView.template_name
    storage_helper = EncryptedStorageHelper
    steps_helper = ReportStepsHelper

    def form_valid(self, form):
        if self.storage.secret_key:
            self._save_report()
        return super().form_valid(form)

    def _save_report(self):
        self.report.encrypt_report(
            str(self.storage.form_data),
            self.storage.secret_key,
        )

encrypt report data as a string