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


class sfquery(object):
    def __init__(self):
        self.cobject = []

    def retrieve(self, options):
        queries = {}
        queries["get_data_columns"] = options["datacolumn_query"]
        queries["get_all_columns"] = options["allcolumn_query"]
        queries["get_data"] = options["get_data"]
        queries["minus_query"] = options["minus_query"]
        queries["get_ddl"] = options["get_fromddl"]
        queries["get_tablelist"] = options["get_tablelist"]
        # queries['local_minus_query'] = options['local_minus_query']
        return queries

    def generate_query(
        incoming_query,
        schema,
        tablename,
        start_date,
        end_date,
        use_daterange,
        database_name,
        columnlist,
    ):
        if use_daterange == "0":
            outgoing_query = (
                incoming_query.replace("r_impacted_tables.SCHEM", schema)
                .replace("r_impacted_tables.TBL", tablename)
                .replace("startdate", start_date)
                .replace("sfenv", database_name)
                .replace("v_column_list", columnlist)
            )
        else:
            outgoing_query = (
                incoming_query.replace("r_impacted_tables.SCHEM", schema)
                .replace("r_impacted_tables.TBL", tablename)
                .replace("startdate", start_date)
                .replace("enddate", end_date)
                .replace("sfenv", database_name)
                .replace("v_column_list", columnlist)
            )
        return outgoing_query

    def generate_minusquery(object, minustable, data_columns, fromtable, totable):
        query = (
            object.replace("minustable", minustable)
            .replace("v_column_list", ",".join(data_columns))
            .replace(
                "fromsfenv.r_impacted_tables.SCHEM.r_impacted_tables.TBL", fromtable
            )
            .replace("tosfenv.r_impacted_tables.SCHEM.r_impacted_tables.TBL", totable)
        )
        return query

    def generate_recoveryquery(
        object, recovertable, columnlist, fromtable, minustable, whereclause
    ):
        query = (
            object.replace("recoverytable", recovertable)
            .replace("v_column_list", columnlist)
            .replace(
                "fromsfenv.r_impacted_tables.SCHEM.r_impacted_tables.TBL", fromtable
            )
            .replace("minustable", minustable)
            .replace("columnlist", whereclause)
        )
        return query

    def __del__(self):
        pass
