import sqlite3
from typing import (
    Dict,
    List,
)

from ._CursorIterator import CursorIterator


class Connection(object):
    def __init__(
        self,
        conn_string: str,
        procs_dict: Dict[str, List[str]],
    ):
        self.conn = sqlite3.connect(database=conn_string, uri=True)
        self.procs_dict = procs_dict or {}

    @staticmethod
    def get_number_of_instances():
        return 0

    def transaction_start(self):
        return

    def transaction_end(self):
        return

    def query(self, sql):
        return

    def select(self, sql):
        cursor = self.conn.cursor()
        cursor.execute(sql)
        return CursorIterator(cursor)

    def pselect(self, procedure, **kwargs):
        if procedure not in self.procs_dict:
            raise RuntimeError(f"Procedure {procedure} not found")

        queries = self.procs_dict[procedure]
        for query in queries:
            for arg in kwargs:
                query = query.replace(f":{arg}", str(kwargs[arg]))

            self.conn.execute(query)

    def close(self):
        self.conn.close()
