# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
from pathlib import Path
import sys
from KeyAssigningWidget import Ui_Form

class FileAction():
    
    def __init__(self,ActionString,srcFile,*param):
        self.ActionString = ActionString
        self.srcFile = srcFile
        
    def movesrcLsttoDstPath(self,srcFileName,DstPath):
        pass
    def perform():
        if not Path(self.srcFile).is_file():
            return
        if self.ActionString == 'move':
            p = Path(param[0])
            assert p.is_dir()
            self.movesrcLsttoDstPath(self.srcFile,p)
            


class ClickableLabel(QtWidgets.QLabel):
    clicked = QtCore.pyqtSignal()
    CtrlClicked = QtCore.pyqtSignal()
    Rclicked = QtCore.pyqtSignal()
    CtrlRclicked = QtCore.pyqtSignal()
    def mouseReleaseEvent(self, QMouseEvent):
        print(QMouseEvent)
        modifiers = QtWidgets.QApplication.keyboardModifiers()
        # import pdb; pdb.set_trace()
        leftPressed = QMouseEvent.button() == QtCore.Qt.LeftButton
        rightPressed = QMouseEvent.button() == QtCore.Qt.RightButton
        ctrlPressed = modifiers == QtCore.Qt.ControlModifier
        if leftPressed and not ctrlPressed:
            self.clicked.emit()
        if rightPressed and not ctrlPressed :
            self.Rclicked.emit()
        if leftPressed and ctrlPressed:
            self.CtrlClicked.emit()
        if rightPressed and ctrlPressed:
            self.CtrlRclicked.emit()
        
class Ui_MainWindow(object):
    path = sys.argv[1]
    listI = []
    currentIndex = 0
    def setupList(self):
        self.listI = [str(x) for x in Path(self.path).glob('*.jpg')]

    def noteItDown(self, fileName):
        with open('list' + str(fileName) + '.opml', 'a+') as fp:
            fp.write(self.listI[self.currentIndex] + '\n')
            
        
    def changePicture(self,changeIndex):
        print('Doing Something', changeIndex)
        self.currentIndex += changeIndex
        redhotmap = QtGui.QPixmap(self.listI[self.currentIndex])
        self.h = MainWindow.geometry().height()
        self.w = MainWindow.geometry().width()
        self.label.setGeometry(QtCore.QRect(0, 0, self.w, self.h))
        # redhotmap = redhotmap.scaledToHeight(self.h)
        redhotmap = redhotmap.scaled(self.w,self.h,1)
        self.label.setPixmap(redhotmap)
        with open('scanned.opml', 'a+') as fp:
            for x in self.listI[self.currentIndex : self.currentIndex + changeIndex]:
                fp.write(x + '\n')
        
    def keyPressEvent(self, buttonSeq, MainWindow, action):
        
        QtWidgets.QShortcut(QtGui.QKeySequence(buttonSeq), MainWindow, activated=action)
        
    
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1900, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = ClickableLabel(self.centralwidget)
        self.h = MainWindow.geometry().height()
        h = self.h
        self.w = MainWindow.geometry().width()
        w = self.w
        self.setupList()
        # import pdb; pdb.set_trace()
        self.label.setGeometry(QtCore.QRect(0, 0, w, h))
        self.label.setText("")
        self.label.setScaledContents(False)
        redhotmap = QtGui.QPixmap(self.listI[0])
        # redhotmap = QtGui.QPixmap("D:/paradise/stuff/Images/walls/juggernaut1_huc656199de66b421ef22ad489780ef428_234989_1920x1080_resize_q75_box.jpg")
        redhotmap = redhotmap.scaled(self.w,self.h,1)
        self.label.setPixmap(redhotmap)
        self.label.setWordWrap(False)
        self.label.setObjectName("label")
        self.label.clicked.connect(lambda :self.noteItDown('Lclick'))
        self.label.Rclicked.connect(lambda :self.noteItDown('Rclick'))
        self.keyPressEvent(QtCore.Qt.Key_Left,MainWindow,lambda :self.noteItDown(QtCore.Qt.Key_Left))
        # self.label.CtrlRclicked.connect(lambda :self.changePicture(-20))
        # self.label.CtrlClicked.connect(lambda :self.changePicture(20))
        
        QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Down), MainWindow, activated=lambda :self.changePicture(1))
        QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Up), MainWindow, activated=lambda :self.changePicture(-1))
        QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.CTRL + QtCore.Qt.Key_Down), MainWindow, activated=lambda :self.changePicture(20))
        QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.CTRL + QtCore.Qt.Key_Up), MainWindow, activated=lambda :self.changePicture(-20))
        QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_F1), MainWindow, activated=self.reAssignKey)
        
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        
    def reAssignKey(self):
        self.ui = Ui_Form()
        self.ui.setupUi()
        self.ui.show()
    
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


if __name__ == "__main__":
    
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
