"""
Copyright 2018 Attunity
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

"""
Copyright 2018 Attunity
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

# coding: utf-8
import os, datetime, fileinput, shutil
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import configparser
import subprocess

def emailsupport(messagetype, messagefile):
    """Send to Email"""
    me = GGENV = "Qlik_Admin@LibertyMutual.com"
    you = ['HTTP_509@LibertyMutual.com', 'PMEDW_Prod_Support@LibertyMutual.com', 'QLIK_ADMIN@LibertyMutual.com']
    #you = ['Wendell.Jones@libertymutual.com']
    #you = CONFIG.get('global', 'receivers').split()

    # Open a plain text file for reading.  For this example, assume that
    # the text file contains only ASCII characters.
    message_part = open(messagefile, 'r')
    part2 = MIMEText(message_part.read(), 'html')
    # Create a text/plain message
    message_part.close()

    msg = MIMEMultipart('alternative')

    msg['Subject'] = messagetype

    msg['From'] = me
    msg['To'] = ",".join(you)

    msg.attach(part2)

    # Send the message via our own SMTP server, but don't include the envelope header.
    sendmail(me, you, msg.as_string())


# Set header automation statement for email
def openemail(emailfile):
    emailfile.write("<p>THIS IS AN AUTOMATED NOTIFICATION. PLEASE DO NOT RESPOND TO THIS E-MAIL.</p>")
    emailfile.write("<html>\n")
    emailfile.write("<table border='1'>\n")


# Set footer automation statement for email
def closeemail(emailfile):
    emailfile.write("</table>\n")
    emailfile.write("<p>THIS IS AN AUTOMATED NOTIFICATION. PLEASE DO NOT RESPOND TO THIS E-MAIL.</p>")
    emailfile.write("\t</body>")
    emailfile.write("</html>\n")


def sendmail(me, you, mailmsg):
    # Send the message via our own SMTP server, but don't include the envelope header.
    try:
        s = smtplib.SMTP('smtprelay.lmig.com')
        s.sendmail(me, you, mailmsg)
        s.quit()
    except smtplib.SMTPException:
        print('Email was drafted but I was unable to send it. Unable to connect to mailserver')
        return 1
    else:
        return 0


CONFIG = configparser.RawConfigParser(allow_no_value=True)
CONFIG.read('settings.ini')
minutes = '180'

nowstamp = datetime.datetime.now()
script_name = os.path.basename(__file__)
temp_script_dir = os.path.realpath(__file__)
script_dir = temp_script_dir.replace(script_name, '')
print(script_dir)
ps_template = f'{script_dir}error_check_template.ps1'
print(f'Starttime: {nowstamp}')

# Server changes
server_change_codes = ['300','303','320']
# Server erors/warnings
server_error_codes = ['301','302','321','322','323','340']
# Normal Task Events
task_change_codes = ['400','401','402','403','404','405','507','508']
# Error/Warning Task Events
task_error_codes = ['261','263','406','430','431','432','433','434','435']

# Table Suspended
suspend_codes = ['502','506']

for event_id in server_error_codes:
    ps_file = f'{event_id}_error_check.ps1'
    shutil.copy(ps_template, ps_file)
    # with fileinput.FileInput(ps_file, inplace=True, backup=f'_bak') as file:
    with fileinput.FileInput(ps_file, inplace=True) as file:
        for line in file:
            if '<minutes>' in line:
                newline = line.replace('<minutes>', minutes)
                print(newline.rstrip())
            elif '<event_id>' in line:
                newline = line.replace('<event_id>', event_id)
                print(newline.rstrip())
            else:
                print(line.rstrip())
    subprocess.run(["powershell.exe", f".\{ps_file}"], shell=True)
    os.remove(ps_file)
for event_id in task_error_codes:
    ps_file = f'{event_id}_error_check.ps1'
    shutil.copy(ps_template, ps_file)
    # with fileinput.FileInput(ps_file, inplace=True, backup=f'_bak') as file:
    with fileinput.FileInput(ps_file, inplace=True) as file:
        for line in file:
            if '<minutes>' in line:
                newline = line.replace('<minutes>', minutes)
                print(newline.rstrip())
            elif '<event_id>' in line:
                newline = line.replace('<event_id>', event_id)
                print(newline.rstrip())
            else:
                print(line.rstrip())
    subprocess.run(["powershell.exe", f".\{ps_file}"], shell=True)
    #os.remove(ps_file)
for event_id in suspend_codes:
    ps_file = f'{event_id}_error_check.ps1'
    shutil.copy(ps_template, ps_file)
    #with fileinput.FileInput(ps_file, inplace=True, backup=f'_bak') as file:
    with fileinput.FileInput(ps_file, inplace=True) as file:
        for line in file:
            if '<minutes>' in line:
                newline = line.replace('<minutes>', minutes)
                print(newline.rstrip())
            elif '<event_id>' in line:
                newline = line.replace('<event_id>', event_id)
                print(newline.rstrip())
            else:
                print(line.rstrip())
    subprocess.run(["powershell.exe", f".\{ps_file}"], shell=True)
    os.remove(ps_file)

file_stats = os.stat('events.log')
if file_stats.st_size > 0:
    events = ''
    eventfile = open('events.log', 'r')
    for line in eventfile:
        #print(line.rstrip())
        if len(line) > 23:
            events += f"{line.rstrip()}<br/>\n"
    eventfile.close()
#    print(events)
#    exit()
    environments = []
    timestamp = nowstamp.strftime("%m/%d/%Y, %H:%M:%S")
    html_report = open('error_template.html', 'r')
    latency_list = []
    latency = open('error.html', 'w')
    openemail(latency)
    for line in html_report:
        if 'DateNow' in line:
            latency.write(line.replace('DateNow', timestamp))
        elif 'ContentHere' in line:
            latency.write(line.replace('ContentHere', f'<p style="background: #B6DCFC;">{events}</p>'))
        else:
            latency.write(line)
    closeemail(latency)
    latency.close()
    subjectenvironment = ' & '.join(environments)
    emailsupport(f'ERROR EVENT(S) DETECTED IN {subjectenvironment} QLIK REPLICATE', 'error.html')
    print(f'ERROR EVENT(S) DETECTED IN {subjectenvironment} QLIK REPLICATE')
    nowstamp = datetime.datetime.now()
    os.remove('error.html')
os.remove('events.log')
nowstamp = datetime.datetime.now()
print(f'Endtime: {nowstamp}')
#print('errors_detected: ' + str(latency_detected))
