#!/opt/app/cacheDB/python/bin/python3
import cgitb
cgitb.enable()
import cgi
from vertica_getter import VerticaGetter

TABLE_OPEN = '<table id = AllTasks class="table table-striped table-bordered" cellspacing="0" width="100%">'
THEAD_OPEN = '<thead>'
TBODY_OPEN = '<tbody>'
ROW_OPEN = '<tr>'
COLUMN_OPEN = '<td>'
THEAD_ROW_OPEN = '<th>'
TABLE_CLOSE = '</table>'
ROW_CLOSE = '</tr>'
COLUMN_CLOSE = '</td>'
THEAD_CLOSE = '</thead>'
TBODY_CLOSE = '</tbody>'
THEAD_ROW_CLOSE = '</th>'

class ResultModel():
    """ A class to create model for the UI """

    def __init__(self):
        """ The default constructor"""
        pass

    def __init__(self,start_date,end_date,attid):
        """ The contructor takes 2 arguments at the minimum"""
        self.start_date = start_date
        self.end_date = end_date
        self.attid = attid

    def get_data(self):
        """ A method to get data from Vertica based on different conditions"""
        data_obj = VerticaGetter()
        param_list = [self.attid]
        param_list = [str(elem) for elem in param_list]
        res = data_obj.get_task_details_by_id(param_list)
        return res

    def format_default_result(self,db_res):
        """ Receives an object from the database and formats to an html table"""
        res_str =  []
        res_str.append(TABLE_OPEN)
        res_str.append(THEAD_OPEN)
        res_str.append(ROW_OPEN) # header row open
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('ATT ID')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('STATUS')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('SEVERITY')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('TASK DETAILS')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('REMARKS')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('START DATE')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('END DATE')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('TASK FINISH DATE')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('INSERT DATE')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('INSERTED BY')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('UPDATE DATE')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(THEAD_ROW_OPEN)
        res_str.append('UPDATED BY')
        res_str.append(THEAD_ROW_CLOSE)
        res_str.append(ROW_CLOSE)  # header row close
        res_str.append(THEAD_CLOSE)
        res_str.append(TBODY_OPEN)
        for row in db_res:
            res_str.append(ROW_OPEN)
            for elem in row:
                res_str.append(COLUMN_OPEN)
                res_str.append(str(elem))  # do this gracefully
                res_str.append(COLUMN_CLOSE)
            res_str.append(ROW_CLOSE)
        res_str.append(TBODY_CLOSE)
        res_str.append(TABLE_CLOSE)
        return "".join(res_str)

    def default_response(self,response_msg='A pretty minimal response'):
        """ A method to send a default response to the browser for testing purposes"""
        resp = []
        resp.append("Content-Type: text/plain")
        resp.append("")
        resp.append(response_msg)
        res_str = "\r\n".join(resp)
        return res_str

if __name__ == '__main__':
    form = cgi.FieldStorage()
    date = form.getfirst('stDate','2017-06-10')
    end_date = form.getfirst('endDate','2017-06-10')
    attid = form.getfirst('attid','')
    #result_flag = form.getfirsti('aggrFlag','NO')
    model = ResultModel(date,end_date,attid)
    res_list = model.get_data()
    html_res = model.format_default_result(res_list)
    print(model.default_response(html_res))


