from datetime import date, timedelta
import sys

from django.core.management.base import BaseCommand, CommandError
from django.template.loader import get_template
from django.core.mail import send_mail, BadHeaderError, EmailMessage
from django.core.mail import EmailMultiAlternatives
from django.conf import settings

from input.models import Project, Library, HonoraryCertificate, Travel, Email,\
                         BusinessCard, List, IFG, Literature
from input.settings import IF_EMAIL, SURVEYPREFIX, SURVEY_EMAIL

class Command(BaseCommand):
    ''' mails will be send here:

    - two weeks after confirmation of support for volunteer (/extern) send link
    with surveylink

    - same for HonoraryCertificate (/intern)

    - travel: mail 3 weeks after end of project.

    - assumed end of project (/project) reached: mail to IF, link to project-editpage

    - 4 weeks after end of project reached: mail with surveylink
    '''

    help = '''This command sends mail with some links to the database or to the survey
              after some amount of time.'''

    def survey_link(self, email, type, pid, name, realname):
        context = {'realname': realname,
                       'type': type,
                       'name': name,
                       'pid': pid,
                       'SURVEYPREFIX': SURVEYPREFIX, }
        txt_mail_template = get_template('input/survey_mail.txt')
        html_mail_template = get_template('input/survey_mail.html')
        try:
            subject, from_email, to = 'Dein Feedback zur Förderung durch Wikimedia Deutschland', IF_EMAIL, email
            text_content = txt_mail_template.render(context)
            html_content = html_mail_template.render(context)
            msg = EmailMultiAlternatives(subject, text_content, from_email, [to], bcc=[SURVEY_EMAIL])
            msg.attach_alternative(html_content, "text/html")
            msg.send()
            #print('survey mail would have been send')

            #survey_mail = EmailMessage('Dein Feedback zur Förderung durch Wikimedia Deutschland',
            #      mail_template.render(context),
            #      IF_EMAIL,
            #      [email],
            #      bcc=[SURVEY_EMAIL])
            #survey_mail.send(fail_silently=False)
        except BadHeaderError:
            return HttpResponse('Invalid header found.')

        print(f'send surveylinkemail to {email}...')

    ''' the db entry mail_state was added. Useful when migrating databases. first delete the entry, then makemigrations
        then migrate.. after that, recreate the entry with default END, after that makemigrations and migrate. 
        now all entries have the value END. after that rewrite the default to NONE in models.py, then makemigrations
        and migrate again, to have NONE as default for all new queries.  '''


    def end_of_projects_reached(self):
        ''' end of project reached '''
        # get all projects which ended
        
        old = Project.objects.filter(end__lt = date.today())\
                             .exclude(end_mail_send = True)\
                             .filter(mail_state = 'NONE')
        
        txt_mail_template = get_template('input/if_end_of_project.txt')
        html_mail_template = get_template('input/if_end_of_project.html')
        

        for project in old:
            context = {'project': project}
            context['URLPREFIX'] = settings.URLPREFIX
            
            try:
                subject, from_email, to = 'Projektende erreicht', IF_EMAIL, IF_EMAIL
                text_content = txt_mail_template.render(context)
                html_content = html_mail_template.render(context)
                msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
                msg.attach_alternative(html_content, "text/html")
                msg.send()
                #print('end of project mail would have been sent')

                #send_mail('Projektende erreicht',
                #          mail_template.render(context),
                #          IF_EMAIL,
                #          [IF_EMAIL],
                #          fail_silently=False)
                project.end_mail_send = True
                project.mail_state = 'INF'
                try:
                    project.save()
                except:
                    print( 'For project', project, 'there is possibly no project.start (', project.start, ') class')
            except BadHeaderError:
                self.stdout.write(self.style.ERROR('Invalid header found.'))

        self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.'))

    def end_of_projects_approved(self):
        ''' end of project approved '''
        # get all projects where end was reached already, and send mails for the ones already set to status "ended" by the admins

        approved_end = Project.objects.filter(status = 'END')\
                             .exclude(end_mail_send = True)\
                             .filter(mail_state = 'INF')
        txt_mail_template = get_template('input/if_end_of_project_approved.txt')
        html_mail_template = get_template('input/if_end_of_project_approved.html')

        txt_informMail_template = get_template('input/if_end_of_project_orginformed.txt')
        html_informMail_template = get_template('input/if_end_of_project_orginformed.html')
        # send the mail to project.email, which would be the mail of the volunteer filling out the form
        
        for project in approved_end:
            context = {'project': project}
            context['URLPREFIX'] = settings.URLPREFIX
            

            try:
                subject, from_email, to = 'Projektende erreicht', IF_EMAIL, project.email
                text_content = txt_mail_template.render(context)
                html_content = html_mail_template.render(context)
                msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
                msg.attach_alternative(html_content, "text/html")
                msg.send()
                #print('if and of project approved mail would have been sent')
                
                inform_subject, inform_from_email, inform_to = 'Projektorganisator*in wurde informiert', IF_EMAIL, IF_EMAIL
                inform_text_content = txt_informMail_template.render(context)
                inform_html_content = html_informMail_template.render(context)
                inform_msg = EmailMultiAlternatives(inform_subject, inform_text_content, inform_from_email, [inform_to])
                inform_msg.attach_alternative(html_content, "text/html")
                inform_msg.send()
                #print('if end of project orginformed mail would have been sent')

                #send_mail('Projektende erreicht',
                #          mail_template.render(context),
                #          IF_EMAIL,
                #          [project.email],
                #          fail_silently=False)
                #send_mail('Projektorganisator*in wurde informiert',
                #          informMail_template.render(context),
                #          IF_EMAIL,
                #          [IF_EMAIL],
                #          fail_silently=False)
                project.end_mail_send = True
                project.mail_state = 'CLOSE'
                try:
                    project.save()
                except:
                    print( 'For project', project, 'there is possibly no project.start (', project.start, ') class')
            except BadHeaderError:
                self.stdout.write(self.style.ERROR('Invalid header found.'))

        self.stdout.write(self.style.SUCCESS('end_of_projects_approved() executed.'))

    def notHappened_of_projects_approved(self):
        ''' notHappened of project approved '''
        # get all projects where end was reached already, and send mails for the ones where status was put to NOT by admins

        approved_notHappened = Project.objects.filter(status = 'NOT')\
                             .exclude(end_mail_send = True)\
                             .filter(mail_state = 'INF')

        html_mail_template = get_template('input/if_not_of_project_approved.html')
        txt_mail_template = get_template('input/if_not_of_project_approved.txt')
        
        txt_informMail_template = get_template('input/if_end_of_project_orginformed.txt')
        html_informMail_template = get_template('input/if_end_of_project_orginformed.html')
        # send the mail to project.email, which would be the mail of the volunteer that filled out the form

        for project in approved_notHappened:
            context = {'project': project}
            context['URLPREFIX'] = settings.URLPREFIX
            try:
                subject, from_email, to = 'Projektende erreicht', IF_EMAIL, project.email
                text_content = txt_mail_template.render(context)
                html_content = html_mail_template.render(context)
                msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
                msg.attach_alternative(html_content, "text/html")
                msg.send()
                #print('if not of project approved end mail would have been sent')


                #send_mail('Projektende erreicht',
                #          mail_template.render(context),
                #          IF_EMAIL,
                #          [project.email],
                #          fail_silently=False)
                
                inform_subject, inform_from_email, inform_to = 'Projektorganisator*in wurde informiert', IF_EMAIL, IF_EMAIL
                inform_text_content = txt_informMail_template.render(context)
                inform_html_content = html_informMail_template.render(context)
                inform_msg = EmailMultiAlternatives(inform_subject, inform_text_content, inform_from_email, [inform_to])
                inform_msg.attach_alternative(html_content, "text/html")
                inform_msg.send()
                #print('if not of project approved end mail orginformed would have been sent')

                #send_mail('Projektorganisator*in wurde informiert',
                #          informMail_template.render(context),
                #          IF_EMAIL,
                #          [IF_EMAIL],
                #          fail_silently=False)
                project.end_mail_send = True
                project.mail_state = 'CLOSE'
                project.save()
            except BadHeaderError:
                self.stdout.write(self.style.ERROR('Invalid header found.'))

        self.stdout.write(self.style.SUCCESS('notHappened_of_projects_approved() executed.'))


    def surveymails_to_object(self, supported, name='', type='LIB'):
        mytype=type
        myname = name
        for item in supported:
            if type == 'LIB':
                mytype = item.type
            elif type not in ('MAIL','VIS','LIST'):
                myname = getattr(item, name, 'ERROR: NONAME')
                print(f'name gefunden: {myname}')
            self.survey_link(email=item.email,
                         type=mytype,
                         pid=f'{mytype}{item.pk}',
                         name=myname,
                         realname=item.realname)
            item.survey_mail_send = True
            item.mail_state = 'END'
            item.survey_mail_date = date.today()
            item.save()
        self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} sent'))


    ''' TODO: there could be some more removing of duplicated code in the following functions '''

    def surveymails_to_lib(self):
        '''get all library objects which where granted two weeks ago'''

        supported = Library.objects.filter(granted=True)\
                                   .filter(granted_date__lt = date.today() - timedelta(days=14))\
                                   .exclude(survey_mail_send=True)\
                                   .exclude(mail_state = 'END')
        self.surveymails_to_object(supported,name='library')

    def surveymails_to_hon(self):
        '''get all HonoraryCertificate objects which where granted two weeks ago'''

        supported = HonoraryCertificate.objects.filter(granted=True)\
                                   .filter(granted_date__lt = date.today() - timedelta(days=14))\
                                   .exclude(survey_mail_send=True)\
                                   .exclude(mail_state = 'END')
        self.surveymails_to_object(supported, type='HON', name='project')

    def surveymails_to_ifg(self):
        '''get all IFG objects which where granted two weeks ago'''

        supported = IFG.objects.filter(granted=True)\
                                   .filter(granted_date__lt = date.today() - timedelta(days=14))\
                                   .exclude(survey_mail_send=True)\
                                   .exclude(mail_state = 'END')
        self.surveymails_to_object(supported, type='IFG', name='url')

    def surveymails_to_lit(self):
        '''get all Litearure objects which where granted two weeks ago'''

        supported = Literature.objects.filter(granted=True)\
                                   .filter(granted_date__lt = date.today() - timedelta(days=14))\
                                   .exclude(survey_mail_send=True)\
                                   .exclude(mail_state = 'END')
        self.surveymails_to_object(supported, type='LIT', name='info')

    def surveymails_to_project(self):
        '''send survey link 4 weeks after end of project reached'''
        supported = Project.objects.filter(granted=True)\
                                   .filter(end__lt = date.today() - timedelta(days=28))\
                                   .exclude(survey_mail_send=True)\
                                   .exclude(mail_state = 'END')
        self.surveymails_to_object(supported, type='PRO', name='name')

    def surveymails_to_travel(self):
        '''send survey link 3 weeks after end of project reached'''

        supported = Travel.objects.filter(project__granted=True)\
                                   .filter(project__end__lt = date.today() - timedelta(days=21))\
                                   .exclude(survey_mail_send=True)\
                                   .exclude(mail_state = 'END')
        self.surveymails_to_object(supported, type='TRAV', name='project')

    def surveymails_to_mail_vis_lis(self):
        '''send survey link 2 weeks after mailadresss, mailinglist or businesscards are granted'''
        lastdate = date.today() - timedelta(days=14)

        typefield = ('MAIL','VIS','LIST')
        count = 0
        for c in ('Email', 'BusinessCard', 'List'):
            # get class via string
            supported = getattr(sys.modules[__name__], c).objects.filter(granted=True)\
                                       .filter(granted_date__lt = lastdate)\
                                       .exclude(survey_mail_send=True)\
                                       .exclude(mail_state = 'END')
            self.surveymails_to_object(supported, type=typefield[count])
            count += 1


    def handle(self, *args, **options):
        '''the main function which is called by the custom command'''

        self.end_of_projects_reached()
        self.end_of_projects_approved()
        self.notHappened_of_projects_approved()
        self.surveymails_to_lib()
        self.surveymails_to_hon()
        self.surveymails_to_ifg()
        self.surveymails_to_lit()
        self.surveymails_to_project()
        self.surveymails_to_travel()
        self.surveymails_to_mail_vis_lis()

        self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))