from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QTreeWidgetItem

from core import db
from core.repositories import SessionRepository, SessionFolderRepository
from ui.qt.dlg_playlist import DlgPlaylist
from ui.qt.dlg_select_playlist_ui import Ui_dlgSelectPlaylist


class DlgSelectPlaylist(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.createWidgets()

    def createWidgets(self):
        self.ui = Ui_dlgSelectPlaylist()
        self.ui.setupUi(self)

        self.ui.btnAddFolder.clicked.connect(self.addFolder)
        self.ui.btnAdd.clicked.connect(self.createPlaylist)
        self.ui.btnEdit.clicked.connect(self.editSelectedPlaylist)
        self.ui.btnDuplicate.clicked.connect(self.duplicateSelectedPlaylist)
        self.ui.btnDelete.clicked.connect(self.deleteSelectedPlaylist)

        self.ui.tree.itemClicked.connect(self.playlistSelected)

        self.ui.btnSelect.clicked.connect(self.ok)
        self.ui.tree.itemDoubleClicked.connect(self.ok)

        self.ui.tree.setColumnCount(3)
        self.ui.tree.setColumnWidth(0, 54)
        self.ui.tree.hideColumn(2)

        self.populate()

    def populate(self):
        db_session = db.Session()
        session_folder_repo = SessionFolderRepository(db_session)
        session_repo = SessionRepository(db_session)

        self.ui.tree.clear()

        folders = session_folder_repo.get_all()
        folders_index = {}
        sessions = session_repo.get_all()

        self.ui.tree.setUpdatesEnabled(False)

        for folder in folders:
            item = QTreeWidgetItem(self.ui.tree)
            item.setIcon(0, QIcon(":/img/rsc/folder.png"))
            item.setText(1, folder.name)
            folders_index[folder.id] = item

        for session in sessions:
            if session.folder_id:
                parent = folders_index[session.folder_id]
            else:
                parent = self.ui.tree

            item = QTreeWidgetItem(parent)
            item.setIcon(0, QIcon(":/img/rsc/playlist.png"))
            item.setText(1, session.name)
            item.setData(2, 0, session.id)

        self.ui.tree.setUpdatesEnabled(True)

    def createPlaylist(self):
        r = DlgPlaylist.edit(self)
        if r:
            self.populate()

    def addFolder(self):
        pass

    def editSelectedPlaylist(self):
        current = self.selected_playlist()
        if not current:
            return
        r = DlgPlaylist.edit(self, current)
        if r:
            self.populate()

    def duplicateSelectedPlaylist(self):
        pass

    def deleteSelectedPlaylist(self):
        pass

    @classmethod
    def select(cls, parent):
        dlg = cls(parent)
        r = dlg.exec_()
        if r:
            return dlg.selected_playlist()
        else:
            return None

    def ok(self):
        self.done(1)

    def selected_playlist(self):
        session_id = self.ui.tree.selectionModel().selection().indexes()[2].data(2)
        if not session_id:
            return None
        return SessionRepository().get_by_id(int(session_id))

    def playlistSelected(self):
        self.ui.btnSelect.setEnabled(True)