"""
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 sqlite3
from base64 import b64decode

import psycopg2
from snowflake import connector


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

    def connect(self, sfparams):
        account = sfparams[0]
        user = sfparams[1]
        pw = bytes(sfparams[2], "UTF8")
        npw = b64decode(pw).decode("UTF8")
        password = npw
        warehouse = sfparams[3]
        database = sfparams[4]
        schema = sfparams[5]
        try:
            conn = connector.connect(
                user=user,
                password=password,
                account=account,
                warehouse=warehouse,
                database=database,
                schema=schema,
            )
            conn._paramstyle = "qmark"
        except Exception as e:
            print(f"Unable to connect to schema: {database}.{schema}:")
            print(str(e))
        else:
            print(f"Connected to {database}.{schema} in warehouse: {warehouse}.")
            return (database, schema, conn)

    def sfrecover(self, sfparams):
        account = sfparams[0]
        user = sfparams[1]
        pw = bytes(sfparams[2], "UTF8")
        npw = b64decode(pw).decode("UTF8")
        password = npw
        warehouse = sfparams[3]
        database = sfparams[4]
        schema = sfparams[5]
        try:
            conn = connector.connect(
                user=user,
                password=password,
                account=account,
                warehouse=warehouse,
                database=database,
                schema=schema,
            )
            conn._paramstyle = "qmark"
        except Exception as e:
            print(f"Unable to connect to schema: {database}.{schema}:")
            print(str(e))
        else:
            print(f"Connected to {database}.{schema} in warehouse: {warehouse}.")
            return conn

    def open_sqlite(self, dbfile):
        utilconn = sqlite3.connect(dbfile)
        utilcur = utilconn.cursor()
        return (utilconn, utilcur)

    def open_pgsql(self, schema):
        try:
            conn = psycopg2.connect(
                dbname='recovery',
                user='postgres',
                host='postgres',
                password='Optimus0329',
                options=f'-c search_path={schema}'
            )
            cur = conn.cursor()
            return (conn, cur)
        except:
            print(f"Unable to connect to the database")

    def __del__(self):
        pass
