import sqlite3
import urllib.request
from functools import cache

from PyQt5 import QtWidgets, QtGui, QtCore
from numpy.random import randint

from gui_qt._py.pokewindow import Ui_Dialog
from gui_qt._py.pokeentry import Ui_Form


def get_connection(db):
    connection = sqlite3.connect(db)
    connection.row_factory = sqlite3.Row

    return connection


class QPoke(QtWidgets.QDialog, Ui_Dialog):
    types = {None: ('', '')}
    stats = {}

    def __init__(self, parent=None):
        super().__init__(parent=parent)

        from importlib.resources import path
        with path('resources', 'pokemon.sqlite') as fp:
            self._db = str(fp)
            PokemonEntry._db = str(fp)

        self.setupUi(self)

        self._fetch_names()

        self.add_pokedexes()

        self.tableWidget_2.itemSelectionChanged.connect(self.show_pokemon)

        self.comboBox_2.currentIndexChanged.connect(self.collect_pokemon)
        self.comboBox.currentIndexChanged.connect(self.collect_pokemon)

        self.pushButton.clicked.connect(self.randomize)

        self.collect_pokemon()

    def _fetch_names(self):
        connection = get_connection(self._db)
        cursor = connection.cursor()
        cursor.execute(
            'SELECT * FROM types'
        )

        self.comboBox.addItem('All types', -1)
        for entry in cursor.fetchall():
            self.types[entry['id']] = (entry['name_de'], entry['name_en'])
            self.comboBox.addItem(entry['name_en'], entry['id'])

        connection.close()

    def add_pokedexes(self):
        connection = get_connection(self._db)
        cursor = connection.cursor()
        cursor.execute('SELECT * FROM pokedex_list')

        for entry in cursor.fetchall():
            self.comboBox_2.addItem(entry['name_en'], entry['id'])

        connection.close()

    def fill_list(self, idx: int = 0):
        pokedex_id = self.comboBox_2.itemData(idx, QtCore.Qt.ItemDataRole.UserRole)
        pokedex = self.get_pokedex(pokedex_id)

        self.tableWidget_2.clear()
        self.tableWidget_2.setColumnCount(4)
        self.tableWidget_2.setRowCount(0)

        for pokemon in pokedex:
            poke_id = pokemon['species_id']
            row_count = self.tableWidget_2.rowCount()
            self.tableWidget_2.setRowCount(row_count + 1)
            item = QtWidgets.QTableWidgetItem(f'#{pokemon["entry_number"]:04d}')
            item.setData(QtCore.Qt.ItemDataRole.UserRole, poke_id)
            self.tableWidget_2.setItem(row_count, 0, item)

            item = QtWidgets.QTableWidgetItem(pokemon['name_en'])
            item.setToolTip(pokemon['name_de'])
            self.tableWidget_2.setItem(row_count, 1, item)

            poke_type = self.types[pokemon['type1']]
            item = QtWidgets.QTableWidgetItem(poke_type[1])
            item.setToolTip(poke_type[0])
            self.tableWidget_2.setItem(row_count, 2, item)

            try:
                poke_type = self.types[pokemon['type2']]
                item = QtWidgets.QTableWidgetItem(poke_type[1])
                item.setToolTip(poke_type[0])
                self.tableWidget_2.setItem(row_count, 3, item)
            except KeyError:
                pass

        self.tableWidget_2.resizeColumnsToContents()

    def collect_pokemon(self, *args):
        pokedex_id = self.comboBox_2.currentData(QtCore.Qt.ItemDataRole.UserRole)
        type_id = self.comboBox.currentData(QtCore.Qt.ItemDataRole.UserRole)

        connection = get_connection(self._db)
        cursor = connection.cursor()

        if pokedex_id == 1:
            if type_id == -1:
                cursor.execute(
                    'SELECT *, pokemon_list.species_id AS entry_number '
                    'FROM pokemon_list '
                    'ORDER BY entry_number'
                )
            else:
                cursor.execute(
                    'SELECT *, pokemon_list.species_id AS entry_number '
                    'FROM pokemon_list '
                    'WHERE pokemon_list.type1 = ? OR pokemon_list.type2 = ? '
                    'ORDER BY entry_number',
                    (type_id, type_id)
                )
        else:
            if type_id == -1:
                cursor.execute(
                    'SELECT pokemon_list.*, pp.entry_number '
                    'FROM pokemon_list '
                    'JOIN pokedex_pokemon pp ON pp.species_id = pokemon_list.species_id '
                    'WHERE pp.pokedex_id = ? '
                    'ORDER BY pp.entry_number',
                    (pokedex_id,)
                )
            else:
                cursor.execute(
                    'SELECT pokemon_list.*, pp.entry_number '
                    'FROM pokemon_list '
                    'JOIN pokedex_pokemon pp ON pp.species_id = pokemon_list.species_id '
                    'WHERE pp.pokedex_id = ? AND (pokemon_list.type1 = ? OR pokemon_list.type2 = ?) '
                    'ORDER BY pp.entry_number',
                    (pokedex_id, type_id, type_id)
                )

        result = cursor.fetchall()
        connection.close()

        self.fill_sorter(result)

    def fill_sorter(self, result):
        self.tableWidget_2.clearContents()
        self.tableWidget_2.setRowCount(0)

        self.tableWidget_2.setSortingEnabled(False)

        for entry in result:
            row = self.tableWidget_2.rowCount()
            self.tableWidget_2.setRowCount(row+1)

            item = QtWidgets.QTableWidgetItem(f"{entry['entry_number']:04d}")
            item.setData(QtCore.Qt.ItemDataRole.UserRole, entry['species_id'])
            item.setData(QtCore.Qt.ItemDataRole.UserRole+1, entry['pokemon_id'])
            self.tableWidget_2.setItem(row, 0, item)

            name_en = entry['name_en']
            if entry['full_name_en']:
                name_en = entry['full_name_en']
            elif entry['form_en']:
                name_en += f" {entry['form_en']}"

            name_de = entry['name_de']
            if entry['full_name_de']:
                name_de = entry['full_name_de']
            elif entry['form_de']:
                name_de += f" {entry['form_de']}"

            item = QtWidgets.QTableWidgetItem(name_en)
            item.setToolTip(name_de)
            self.tableWidget_2.setItem(row, 1, item)

            type_en = []
            type_de = []
            for t_id in ('type1', 'type2'):
                t_de, t_en = self.types[entry[t_id]]
                if t_en:
                    type_en.append(t_en)
                    type_de.append(t_de)

            item = QtWidgets.QTableWidgetItem(' / '.join(type_en))
            item.setToolTip('\n'.join(type_en))
            self.tableWidget_2.setItem(row, 2, item)

            total = 0
            for i, stat_name in enumerate(('hit_points', 'attack', 'defense', 'sp_atk', 'sp_def', 'speed')):
                stat_value = entry[stat_name]
                item = QtWidgets.QTableWidgetItem()
                item.setData(QtCore.Qt.ItemDataRole.DisplayRole, stat_value)
                item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignRight | QtCore.Qt.AlignmentFlag.AlignVCenter)
                self.tableWidget_2.setItem(row, i+4, item)
                total += stat_value

            item = QtWidgets.QTableWidgetItem()
            item.setData(QtCore.Qt.ItemDataRole.DisplayRole, total)
            item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignRight | QtCore.Qt.AlignmentFlag.AlignVCenter)
            self.tableWidget_2.setItem(row, 3, item)

            item = QtWidgets.QTableWidgetItem()
            item.setData(QtCore.Qt.ItemDataRole.DisplayRole, entry['height'] / 10)
            item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignRight | QtCore.Qt.AlignmentFlag.AlignVCenter)
            self.tableWidget_2.setItem(row, 10, item)

            item = QtWidgets.QTableWidgetItem()
            item.setData(QtCore.Qt.ItemDataRole.DisplayRole, entry['weight'] / 10)
            item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignRight | QtCore.Qt.AlignmentFlag.AlignVCenter)
            self.tableWidget_2.setItem(row, 11, item)

            item = QtWidgets.QTableWidgetItem()
            item.setData(QtCore.Qt.ItemDataRole.DisplayRole, round(entry['weight']/entry['height']**2 * 10, 2))
            item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignRight | QtCore.Qt.AlignmentFlag.AlignVCenter)
            self.tableWidget_2.setItem(row, 12, item)

        self.tableWidget_2.resizeColumnToContents(1)
        self.tableWidget_2.resizeColumnToContents(2)
        self.tableWidget_2.setSortingEnabled(True)

    def randomize(self):
        select = randint(0, self.tableWidget_2.rowCount())
        self.show_pokemon(select)

    def show_pokemon(self, row: int = None):
        table = self.tableWidget_2
        if row is None:
            row = table.currentRow()

        species_id = table.item(row, 0).data(QtCore.Qt.ItemDataRole.UserRole)
        poke_id = table.item(row, 0).data(QtCore.Qt.ItemDataRole.UserRole+1)
        pokemon_name = table.item(row, 1).text()

        connection = get_connection(self._db)
        cursor = connection.cursor()

        cursor.execute(
            'SELECT p.id FROM pokemon p WHERE p.species_id = ?',
            (species_id,)
        )

        pokemon = cursor.fetchall()
        connection.close()

        self.tabWidget.setCurrentIndex(0)
        for i in range(1, self.tabWidget.count()):
            self.tabWidget.setTabVisible(i, False)

        widget_idx = 0

        for i, p in enumerate(pokemon):
            entry_widget = self.tabWidget.widget(i)

            if poke_id == p[0]:
                widget_idx = i

            if entry_widget is None:
                self.tabWidget.addTab(PokemonEntry(p[0]), '')

            self.tabWidget.setTabText(i, pokemon_name)
            self.tabWidget.setTabVisible(i, True)
            name = self.tabWidget.widget(i).create_pokemon(p[0])
            self.tabWidget.setTabText(i, name)
            self.tabWidget.setCurrentIndex(widget_idx)


class PokemonEntry(QtWidgets.QWidget, Ui_Form):
    _db = ''

    def __init__(self, pokemon_id: int, parent=None):
        super().__init__(parent=parent)

        self.setupUi(self)

        self.bars = [
            None, self.hp_bar, self.attack_bar, self.defense_bar, self.spec_attack_bar, self.spec_defense_bar, self.speed_bar
        ]
        self.ability_labels = [None, self.ability1_label, self.ability2_label, self.ability3_label]
        self.type_labels = [None, self.type1_label, self.type2_label]

        self.create_pokemon(pokemon_id)

    def create_pokemon(self, poke_id):
        pokemon = self.get_pokemon(poke_id)
        species = self.get_species(pokemon['species_id'])

        self.nationaldex_label.setText(f"{pokemon['species_id']:04d}")

        self.species_label.setText(species['genus_en'])
        self.species_label.setToolTip(species['genus_de'])

        self.height_label.setText(f"{pokemon['height'] / 10} m")
        self.weight_label.setText(f"{pokemon['weight'] / 10} kg")

        if species['gender_ratio'] == -1:
            gender = "Gender unknown"
        else:
            gender = f"{species['gender_ratio']*12.5:0.2f}% female, {100-species['gender_ratio']*12.5:0.2f}% male"

        self.gender_label.setText(gender)

        if not QtGui.QPixmapCache.find(str(poke_id)):
            image = b''
            if pokemon['artwork'] is not None:
                try:
                    res = urllib.request.urlopen(pokemon['artwork'])
                    image = res.read()
                except:
                    pass

            pixmap = QtGui.QPixmap()
            pixmap.loadFromData(image)
            sc_pixmap = pixmap.scaled(400, 400, QtCore.Qt.AspectRatioMode.KeepAspectRatio)

            QtGui.QPixmapCache.insert(str(poke_id), sc_pixmap)

        self.artwork_label.setPixmap(QtGui.QPixmapCache.find(str(poke_id)))

        stats = self.get_stats(poke_id)
        for (stat_id, stat_value) in stats:
            self.bars[stat_id].setValue(stat_value)

        abilities = self.get_abilities(poke_id)
        for lab in self.ability_labels[1:]:
            lab.setVisible(False)
        for (slot, is_hidden, name_de, name_en) in abilities:
            self.ability_labels[slot].setVisible(True)
            t = name_en
            if is_hidden:
                t += ' (hidden ability)'
            self.ability_labels[slot].setText(t)
            self.ability_labels[slot].setToolTip(name_de)

        form, types = self.get_form_and_type(poke_id)
        for lab in self.type_labels[1:]:
            lab.setVisible(False)

        for (type_id, slot) in types:
            self.type_labels[slot].setVisible(True)
            self.type_labels[slot].setText(QPoke.types[type_id][1])
            self.type_labels[slot].setToolTip(QPoke.types[type_id][0])

        evolutions = self.make_evolution(pokemon['evolution_id'])

        self.tableWidget.clear()
        self.tableWidget.setColumnCount(4)
        self.tableWidget.setRowCount(len(evolutions))

        for i, e in enumerate(evolutions):
            item = QtWidgets.QTableWidgetItem(f'{e[0]} (#{e[1]:04d})')
            self.tableWidget.setItem(i, 0, item)
            item = QtWidgets.QTableWidgetItem('to')
            self.tableWidget.setItem(i, 1, item)
            item = QtWidgets.QTableWidgetItem(f'{e[2]} (#{e[3]:04d})')
            self.tableWidget.setItem(i, 2, item)
            item = QtWidgets.QTableWidgetItem(f'{e[4]}')
            self.tableWidget.setItem(i, 3, item)

        self.tableWidget.resizeColumnsToContents()
        self.tableWidget.resizeRowsToContents()

        if form['full_name_en'] is not None:
            return form['full_name_en']
        elif form['form_en'] is not None:
            return f"{species['name_en']} ({form['form_en']})"
        else:
            return species['name_en']

    @cache
    def get_pokedex(self, pokedex_id):
        connection = get_connection(self._db)
        cursor = connection.cursor()
        cursor.execute(
            'SELECT pokemon_list.*, pp.entry_number  FROM pokemon_list '
            'JOIN pokedex_pokemon pp ON pp.species_id = pokemon_list.species_id '
            'WHERE pp.pokedex_id = ? '
            'ORDER BY pp.entry_number',
            (pokedex_id,)
        )
        res = cursor.fetchall()
        connection.close()

        return res

    @cache
    def get_abilities(self, pokemon_id):
        conn = get_connection(self._db)
        cursor = conn.cursor()
        cursor.execute(
            'SELECT slot, ís_hidden, name_de, name_en FROM pokemon_ability '
            'JOIN main.ability ON pokemon_ability.ability_id = ability.id '
            'WHERE pokemon_id = ?',
            (pokemon_id,)
        )

        abilities = cursor.fetchall()
        conn.close()

        return abilities

    @cache
    def get_pokemon(self, poke_id):
        connection = get_connection(self._db)
        cursor = connection.cursor()

        cursor.execute(
            'SELECT p.species_id, p.height, p.weight, p.artwork, p.evolution_id FROM pokemon p WHERE p.id = ?',
            (poke_id,)
        )
        pokemon = cursor.fetchone()

        connection.close()

        return pokemon

    @cache
    def get_species(self, species_id):
        connection = get_connection(self._db)
        cursor = connection.cursor()

        cursor.execute(
            'SELECT s.id, s.name_en, s.name_de, s.genus_de, s.genus_en, s.color_id, s.is_mythical, s.is_legendary, s.generation, s.gender_ratio '
            'FROM species s '
            'WHERE s.id = ?',
            (species_id,)
        )

        species = cursor.fetchone()

        connection.close()

        return species

    @cache
    def get_stats(self, pokemon_id):
        conn = get_connection(self._db)
        cursor = conn.cursor()

        cursor.execute(
            'SELECT stat_id, value FROM pokemon_stat WHERE pokemon_id = ?',
            (pokemon_id,)
        )
        stats = cursor.fetchall()
        conn.close()

        return stats

    @cache
    def get_form_and_type(self, pokemon_id):
        conn = get_connection(self._db)
        cursor = conn.cursor()
        cursor.execute(
            'SELECT id, full_name_en, form_en FROM form WHERE pokemon_id = ? AND is_default = 1',
            (pokemon_id,)
        )

        form = cursor.fetchone()

        cursor.execute(
            'SELECT type_id, slot FROM form_type WHERE form_id = ?',
            (form['id'],)
        )
        types = cursor.fetchall()

        conn.close()

        return form, types

    @cache
    def make_evolution(self, poke_id: int):
        steps = []

        conn = get_connection(self._db)
        cursor = conn.cursor()

        cursor.execute('SELECT * FROM evolution_names WHERE id = ?', (poke_id,))
        chain = cursor.fetchall()
        conn.close()

        trigger_texts = [
            None,
            'Level up',
            'Trade',
            '',
            'Empty spot in party',
            'Spin',
            'Train in the Tower of Darkness',
            'Train in the Tower of Water',
            'Land three critical hits in a battle',
            'Go somewhere after taking damage',
            '',
            'Use Psyshield Bash 20 times in Agile Style',
            'Use Barb Barrage 20 times in Strong Style',
            'Receive 294 recoil damage in battle',
        ]

        special_pokemon = {
            24: 'Use Rage Fist 20 times',
            317: "Defeat 3 Bisharp that are holding Leader's Crest",
            528: 'Collect 999 Coins from Roaming Form',
            484: "Walk 1,000 steps in Let's Go mode",
            485: "Walk 1,000 steps in Let's Go mode",
            495: "Walk 1,000 steps in Let's Go mode",
            499: "Walk 1,000 steps in Let's Go mode",
            504: "Level up while in multiplayer"
        }

        condition_text = {
            'min_level': lambda x: f"Lv. {x['min_level']}",
            'min_happiness': lambda _: f"high Friendship",
            'min_beauty': lambda _: f"needs max. Beauty",
            'min_affection': lambda x: f"{x['min_affection']} Affection",
            'location_en': lambda x: f"at {x['location_en']}",
            'held_item_en': lambda x: f"hold {x['held_item_en']}",
            'item_en': lambda x: f'Use {x["item_en"]}',
            'known_move_en': lambda x: f"know {x['known_move_en']}",
            'move_type_en': lambda x: f"know {x['move_type_en']} move",
            'party_species_en': lambda x: f"{x['party_species_en']} in party",
            'party_type_en': lambda x: f"{x['party_type_en']} in party",
            'time_of_day': lambda x: f"at {x['time_of_day']}",
            'trade_species_en': lambda x: f"with {x['trade_species_en']}",
            'needs_rain': lambda _: 'during rain',
            'upside_down': lambda _: 'hold controller upside-down',
            'relative_stats': lambda x: {1: 'attack &gt; defense', 0: 'attack = defense', -1: 'attack &lt; defense'}[x['relative_stats']],
        }

        for c in chain:
            lvl0 = c["name_en"]
            if c['gender'] == 1:
                lvl0 += ' (female)'
            elif c['gender'] == 2:
                lvl0 += ' (male)'

            trig = c['trigger']

            if trig == 10:
                level_text = [special_pokemon[poke_id]]
            else:
                level_text = [trigger_texts[trig]]

            for k, v in condition_text.items():
                if c[k] is not None:
                    level_text.append(v(c))

            steps.append(
                (lvl0, c['evolves_from'], c['evolve_en'], c['species_id'], ', '.join(filter(lambda x: x, level_text)))
            )

        return steps
