import json
import sys
import base64
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from __config import CONFIG
from __mySqlConnector import MYSQLCONNECTOR
from datetime import datetime, timezone, timedelta




class DATAHANDLER():

    def __init__(self, inputData):
        inputData = json.loads( base64.b64decode(inputData) ) 
        self.category = inputData["category"]

        self.myConnector = MYSQLCONNECTOR()
        self.debug = ''

    def getData(self):

        historicalDateList = self.myConnector.getHistoricalDateDetails(self.category)
        dateRangeList = []
        for historicalDate in historicalDateList:
            dateRangeList.append(historicalDate[0])
        return dateRangeList

        

    def getDebug(self):
        return str(self.debug)



if __name__== "__main__":

    returnDict = {}
    returnDict['status'] = 'OK'
    returnDict['remarks'] = ''
    returnDict['dateRangeList'] = []

    try:
        handler = DATAHANDLER(sys.argv[1])
    except Exception as e:
        returnDict['status'] = 'KO'
        returnDict['remarks'] = str(e)
        print (json.dumps(returnDict))
        sys.exit()

    try:
        returnDict['dateRangeList'] = handler.getData()
    except Exception as e:
        returnDict['status'] = 'KO'
        returnDict['remarks'] = str(e) + ' ' + handler.getDebug()

    print (json.dumps(returnDict))


    