"""
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.
"""

import configparser
from base64 import b64decode
from encodings import utf_8
from datetime import datetime


class reader:
    def __init__(self):
        self.CONFIG = configparser.RawConfigParser(allow_no_value=True)
        self.options = []
        self.parameters = {}

    def retrieve(self, filename, session):
        self.CONFIG.read(filename)
        self.parameters["startdate"] = session["startdate"]
        self.parameters["enddate"] = session["enddate"]
        if session["startdate"] == session["enddate"]:
            self.parameters["use_daterange"] = 0
        else:
            self.parameters["use_daterange"] = 1
        self.sf_logins = {}
        # for option, value in self.CONFIG.items("sf_targets"):
        #     loginname = option
        #     itemlist = value.split(",")
        #     pw = bytes(itemlist[2], "UTF8")
        #     npw = b64decode(pw).decode("UTF8")
        #     self.sf_logins[loginname] = (
        #         loginname,
        #         [itemlist[0], itemlist[1], npw, itemlist[3], itemlist[4], itemlist[5]],
        #     )
        # self.parameters["logins"] = self.sf_logins
        self.parameters["exclude_list"] = self.CONFIG.get("global", "exclude_list")
        self.parameters["allcolumn_query"] = self.CONFIG.get(
            "query_template", "get_all_columns"
        )
        self.parameters["datacolumn_query"] = self.CONFIG.get(
            "query_template", "get_data_columns"
        )
        self.parameters["minus_query"] = self.CONFIG.get(
            "query_template", "minus_query"
        )
        self.parameters["get_fromddl"] = self.CONFIG.get(
            "query_template", "get_fromddl"
        )
        self.parameters["get_tablelist"] = self.CONFIG.get(
            "query_template", "get_tablelist"
        )
        self.parameters["get_data"] = self.CONFIG.get("query_template", "get_data")
        self.parameters["get_daterange"] = self.CONFIG.get(
            "query_template", "get_daterange"
        )
        self.parameters["qlik_columns"] = self.CONFIG.get("global", "qlik_columns")
        self.parameters["full_qlik_columns"] = self.CONFIG.get(
            "global", "full_qlik_columns"
        )
        self.parameters["gg_columns"] = self.CONFIG.get("global", "gg_columns")
        self.parameters["mssql_gg_columns"] = self.CONFIG.get(
            "global", "mssql_gg_columns"
        )
        self.parameters["fromenv"] = self.CONFIG.get("sync_direction", "FROMENV")
        self.parameters["toenv"] = self.CONFIG.get("sync_direction", "TOENV")
        self.parameters["utilitydb"] = self.CONFIG.get("global", "utilitydb")
        impactedtables = []
        for option, value in self.CONFIG.items("impacted_tables"):
            impactedtables.append(option.upper())
        self.parameters["impacted_tables"] = impactedtables
        self.parameters["log_table"] = self.CONFIG.items("log_table")
        self.parameters["populate_recoverytable"] = self.CONFIG.get(
            "query_template", "populate_recoverytable"
        )
        self.parameters["fromdatafile"] = self.CONFIG.get("global", "fromdata")
        self.parameters["todatafile"] = self.CONFIG.get("global", "todata")
        self.parameters["recovereddatafile"] = self.CONFIG.get(
            "global", "recovereddata"
        )
        now = datetime.now()
        currenttime = now.strftime("%m%d%Y_%H%M%S")
        self.parameters["sqlitedb"] = "./data/recovery_" + currenttime + ".sqlite3"
        self.parameters["logfile"] = "./logs/recovery_" + currenttime + ".log"
        if "PL_PROD" in self.parameters["fromenv"].upper():
            self.parameters["sourceenv"] = "PROD"
        elif "PL_QA" in self.parameters["fromenv"].upper():
            self.parameters["sourceenv"] = "QA"
        else:
            self.parameters["sourceenv"] = "DEV"

        if "PL_PROD" in self.parameters["toenv"].upper():
            self.parameters["targetenv"] = "PROD"
        elif "PL_QA" in self.parameters["toenv"].upper():
            self.parameters["targetenv"] = "QA"
        else:
            self.parameters["targetenv"] = "DEV"

        return self.parameters

    def __del__(self):
        pass
