# -*- coding: utf-8 -*-
from aldryn_client.forms import BaseForm, CharField, NumberField, CheckboxField


class Form(BaseForm):

    email_host = CharField('SMTP Host', initial='localhost')
    email_port = NumberField('SMTP Port', initial=25)
    email_host_user = CharField('SMTP User', initial='')
    email_host_password = CharField('SMTP Password', initial='')
    email_use_tls = CheckboxField('Use TLS', required=False, initial=False)

    default_from_email = CharField('Default from email', initial='', required=False)

    email_host_stage = CharField('SMTP Host used on stage', initial='localhost', required=False)
    email_port_stage = NumberField('SMTP Port used on stage', initial=25, required=False)
    email_host_user_stage = CharField('SMTP User used on stage', initial='', required=False)
    email_host_password_stage = CharField('SMTP Password used on stage', initial='', required=False)
    email_use_tls_stage = CheckboxField('Use TLS used on stage', required=False, initial=False)

    mandrill_api_key = CharField('Mandrill API key', initial='', required=False)

    def to_settings(self, data, settings):
        if settings.get('DEBUG'):
            email_settings = {
                'EMAIL_HOST': data.get('email_host_stage') or data.get('email_host'),
                'EMAIL_HOST_PASSWORD': data.get('email_host_password_stage') or data.get('email_host_password'),
                'EMAIL_HOST_USER': data.get('email_host_user_stage') or data.get('email_host_user'),
                'EMAIL_PORT': data.get('email_port_stage') or data.get('email_port'),
                'EMAIL_USE_TLS': data.get('email_use_tls_stage') or data.get('email_use_tls'),
            }
        else:
            email_settings = {
                'EMAIL_HOST': data.get('email_host'),
                'EMAIL_HOST_PASSWORD': data.get('email_host_password'),
                'EMAIL_HOST_USER': data.get('email_host_user'),
                'EMAIL_PORT': data.get('email_port'),
                'EMAIL_USE_TLS': data.get('email_use_tls'),
            }
            mandrill_api_key = data.get('mandrill_api_key')
            if mandrill_api_key:
                email_settings.update({
                    'EMAIL_BACKEND': 'django_mandrill.mail.backends.mandrillbackend.EmailBackend',
                    'MANDRILL_API_KEY': mandrill_api_key
                })

        if data.get('default_from_email', False):
            email_settings['DEFAULT_FROM_EMAIL'] = data.get('default_from_email')
            email_settings['SERVER_EMAIL'] = data.get('default_from_email')
        settings.update(email_settings)
        return settings

@@ -1076,24 +1076,122 @@
 uired=False)
+%0A    use_mandrill_on_stage = CheckboxField('Use mandrill on stage', required=False, initial=False)
 %0A%0A    def to
@@ -1232,17 +1232,25 @@
        i
-f
+s_stage =
  setting
@@ -1263,16 +1263,36 @@
 'DEBUG')
+%0A        if is_stage
 :%0A      
@@ -2170,28 +2170,25 @@
           %7D%0A
-    
+%0A
         mand
@@ -2243,37 +2243,239 @@
     
-    if mandrill_api_key:%0A    
+can_use_mandrill_on_stage = mandrill_api_key and is_stage and data.get('use_mandrill_on_stage')%0A        can_use_mandrill_on_live = mandrill_api_key and not is_stage%0A        if can_use_mandrill_on_stage or can_use_mandrill_on_live:%0A
     
@@ -2506,20 +2506,16 @@
 pdate(%7B%0A
-    
         
@@ -2617,20 +2617,16 @@
         
-    
 'MANDRIL
@@ -2654,20 +2654,16 @@
 api_key%0A
-    
         

Allow mandrill on stage