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


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

    def override_table(self, orobject, minustable_config):
        if orobject == "QUEUE":
            orobject = "QUEUE_X"
        if minustable_config == 1:
            if "HEADER_CHANGE_MASK" in orobject:
                tempstatement1 = re.sub(
                    r"HEADER_CHANGE_MASK[-,\s\w\(\)\'\n_\d]+HEADER_TIMESTAMP TIMESTAMP_NTZ\(6\) NOT NULL,",
                    "",
                    orobject,
                    flags=re.IGNORECASE,
                )
                minusquery = "INSERT INTO minustable (HEADER_CHANGE_SEQ,HEADER_CHANGE_OPER,data_columns) SELECT HEADER_CHANGE_SEQ,HEADER_CHANGE_OPER,data_columns from sourcetable where whereclause;"
            else:
                orobject = re.sub(
                    r"CHANGE_MASK[-,\s\w\(\)\'\n_\d]+TIMESTAMP TIMESTAMP_NTZ\(6\) NOT NULL,",
                    "",
                    orobject,
                    flags=re.IGNORECASE,
                )
                orobject = re.sub(
                    r'"CHANGE_MASK"[\"-,\s\w\(\)\'\n_\d]+"TIMESTAMP" TIMESTAMP_NTZ\(6\) ,',
                    "",
                    orobject,
                    flags=re.IGNORECASE,
                )
                minusquery = "INSERT INTO minustable (CHANGE_SEQ,CHANGE_OPER,data_columns) SELECT CHANGE_SEQ,CHANGE_OPER,data_columns from sourcetable where whereclause;"
            if "VKX" not in orobject:
                orobject = re.sub(
                    r",\n\tSTG_ODS_CDC_TRANS_TYPE[,\s\w\(\)\'\n-]+primary",
                    ", primary",
                    orobject,
                    flags=re.IGNORECASE,
                )
                orobject = re.sub(
                    r",\n\tODS_CDC_TRANS_TYPE[,\s\w\(\)\'\n-]+primary",
                    ", primary",
                    orobject,
                    flags=re.IGNORECASE,
                )
        orobject = orobject.replace("primary key", " primary key")
        orobject = orobject.replace("PRIMARY KEY", " PRIMARY KEY")
        if minustable_config == 1:
            return (orobject, minusquery)
        else:
            return orobject

    def override_column(self, orobject):
        if "RECOVERED_LANGUAGECODE" in orobject or "LANGUAGE_CODE_MINUSTABLE":
            orobject = orobject.replace("RECOVERED_LANGUAGECODE", "LANGUAGECODE ")
            orobject = orobject.replace(
                "LANGUAGECODE_MINUSTABLEVARCHAR", "LANGUAGECODE VARCHAR"
            )
            orobject = orobject.replace(
                ')"header_change_seq", "header_change_oper")', ""
            )
        return orobject

    def override_ddl(self, orobject, actualtable, newtablename):
        orobject = (
            orobject.replace("TABLE " + actualtable, "TABLE " + newtablename)
            .replace(" COLLATE 'en-ci'", "")
            .replace("\n\t", "")
            .replace("\n", "")
            .replace("create or replace TABLE", "CREATE TABLE IF NOT EXISTS")
            .replace("NOT NULL", "")
        )
        return orobject.upper()

    def __del__(self):
        pass
