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

# Form implementation generated from reading ui file 'qt.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 tools import *

test = 0

class Ui_MainWindow(object):
	def setupUi(self, MainWindow):
		self.now_selecting = 0

		MainWindow.setObjectName("MainWindow")
		MainWindow.resize(800, 600)
		self.window = MainWindow
		#left frame
		self.leftframe = QtWidgets.QFrame(MainWindow)
		self.leftframe.setEnabled(True)
		self.leftframe.setGeometry(QtCore.QRect(0, 0, 300, 601))
		self.leftframe.setStatusTip("")
		self.leftframe.setAutoFillBackground(False)
		self.leftframe.setStyleSheet("background-color: rgb(204, 204, 204);")
		self.leftframe.setFrameShape(QtWidgets.QFrame.StyledPanel)
		self.leftframe.setFrameShadow(QtWidgets.QFrame.Raised)
		self.leftframe.setObjectName("leftframe")

		self.add_a_new_event = QtWidgets.QPushButton(self.leftframe)
		self.add_a_new_event.setGeometry(QtCore.QRect(20, 50, 260, 40))
		self.add_a_new_event.setText("Add a new event")
		# self.add_a_new_event.clicked.connect(self.create_new_empty_block)

		self.secondframe = QtWidgets.QFrame(self.leftframe)
		self.secondframe.setGeometry(QtCore.QRect(10, 100, 280, 490))

		self.block = []

		self.formLayout = QtWidgets.QFormLayout()
		self.groupBox = QtWidgets.QGroupBox()
		self.groupBox.setLayout(self.formLayout)

		self.add_nothing_hint()

		self.scroll = QtWidgets.QScrollArea()
		self.scroll.setWidget(self.groupBox)
		self.scroll.setWidgetResizable(True)

		self.layout = QtWidgets.QVBoxLayout(self.secondframe)
		self.layout.addWidget(self.scroll)

		self.completeness = QtWidgets.QComboBox(self.leftframe)
		self.completeness.setGeometry(QtCore.QRect(220, 20, 70, 20))
		self.completeness.setObjectName("completeness")
		self.completeness.addItems(["default", "Not Started Yet", "In Progress", "Completed"])

		self.sort_by = QtWidgets.QComboBox(self.leftframe)
		self.sort_by.setGeometry(QtCore.QRect(140, 20, 70, 20))
		self.sort_by.setObjectName("sort_by")
		self.sort_by.addItems(["default", "due_date", "completion"])

		self.tag_filter = QtWidgets.QComboBox(self.leftframe)
		self.tag_filter.setGeometry(QtCore.QRect(10, 20, 120, 20))
		self.tag_filter.setObjectName("tag_filter")

		self.rightframe = QtWidgets.QFrame(MainWindow)
		self.rightframe.setGeometry(QtCore.QRect(300, 0, 500, 600))
		self.rightframe.setStyleSheet("background-color: rgb(230, 230, 230);")
		self.rightframe.setFrameShape(QtWidgets.QFrame.StyledPanel)
		self.rightframe.setFrameShadow(QtWidgets.QFrame.Raised)
		self.rightframe.setObjectName("rightframe")
		self.lSubject = QtWidgets.QLabel(self.rightframe)
		self.lSubject.setGeometry(QtCore.QRect(30, 50, 71, 40))
		font = QtGui.QFont()
		font.setFamily("Agency FB")
		font.setPointSize(20)
		self.lSubject.setFont(font)
		self.lSubject.setObjectName("lSubject")
		self.Subject = QtWidgets.QLineEdit(self.rightframe)
		self.Subject.setGeometry(QtCore.QRect(110, 50, 371, 41))
		self.Subject.setObjectName("Subject")
		font = QtGui.QFont()
		font.setFamily("Agency FB")
		font.setPointSize(20)
		self.Subject.setFont(font)
		self.ltags = QtWidgets.QLabel(self.rightframe)
		self.ltags.setGeometry(QtCore.QRect(60, 100, 41, 41))
		font = QtGui.QFont()
		font.setFamily("Agency FB")
		font.setPointSize(16)
		self.ltags.setFont(font)
		self.ltags.setObjectName("ltags")
		self.llevel = QtWidgets.QLabel(self.rightframe)
		self.llevel.setGeometry(QtCore.QRect(60, 150, 41, 41))
		font = QtGui.QFont()
		font.setFamily("Agency FB")
		font.setPointSize(16)
		self.llevel.setFont(font)
		self.llevel.setObjectName("llevel")
		self.tags = QtWidgets.QLineEdit(self.rightframe)
		self.tags.setGeometry(QtCore.QRect(110, 110, 231, 31))
		self.tags.setObjectName("tags")
		font = QtGui.QFont()
		font.setFamily("Agency FB")
		font.setPointSize(16)
		self.tags.setFont(font)
		self.level = QtWidgets.QComboBox(self.rightframe)
		self.level.setGeometry(QtCore.QRect(110, 160, 101, 31))
		self.level.setObjectName("level")
		self.level.addItems(["Easy", "Normal", "Hard"])
		self.lcompletion = QtWidgets.QLabel(self.rightframe)
		self.lcompletion.setGeometry(QtCore.QRect(20, 200, 81, 41))
		font = QtGui.QFont()
		font.setFamily("Agency FB")
		font.setPointSize(16)
		self.lcompletion.setFont(font)
		self.lcompletion.setObjectName("lcompletion")
		self.completion = QtWidgets.QSlider(self.rightframe)
		self.completion.setGeometry(QtCore.QRect(110, 210, 201, 22))
		self.completion.setOrientation(QtCore.Qt.Horizontal)
		self.completion.setObjectName("completion")
		self.ldue_date = QtWidgets.QLabel(self.rightframe)
		self.ldue_date.setGeometry(QtCore.QRect(30, 250, 61, 41))
		font = QtGui.QFont()
		font.setFamily("Agency FB")
		font.setPointSize(16)
		self.ldue_date.setFont(font)
		self.ldue_date.setObjectName("ldue_date")
		self.due_date = QtWidgets.QDateTimeEdit(self.rightframe)
		self.due_date.setGeometry(QtCore.QRect(110, 260, 194, 22))
		self.due_date.setObjectName("due_date")
		self.lcomment = QtWidgets.QLabel(self.rightframe)
		self.lcomment.setGeometry(QtCore.QRect(30, 300, 71, 41))
		font = QtGui.QFont()
		font.setFamily("Agency FB")
		font.setPointSize(16)
		self.lcomment.setFont(font)
		self.lcomment.setObjectName("lcomment")
		self.comment = QtWidgets.QTextEdit(self.rightframe)
		self.comment.setGeometry(QtCore.QRect(110, 310, 371, 201))
		self.comment.setObjectName("comment")
		self.save = QtWidgets.QPushButton(self.rightframe)
		self.save.setGeometry(QtCore.QRect(364, 532, 121, 31))
		self.save.setObjectName("save")
		self.delete = QtWidgets.QPushButton(self.rightframe)
		self.delete.setGeometry(QtCore.QRect(195, 532, 121, 31))
		self.delete.setObjectName("delete")	
		self.complete = QtWidgets.QPushButton(self.rightframe)
		self.complete.setGeometry(QtCore.QRect(60, 532, 121, 31))
		self.complete.setObjectName("delete")	
		self.completion_percentage = QtWidgets.QLabel(self.rightframe)
		self.completion_percentage.setGeometry(QtCore.QRect(320, 210, 51, 21))
		self.completion_percentage.setObjectName("completion_percentage")

		self.completion.valueChanged.connect(self.set_percentage)

		self.retranslateUi(MainWindow)
		QtCore.QMetaObject.connectSlotsByName(MainWindow)

	def retranslateUi(self, MainWindow):
		_translate = QtCore.QCoreApplication.translate
		MainWindow.setWindowTitle(_translate("MainWindow", "Progress Indicator"))
		#self.add_a_new_event.setText(_translate("MainWindow", "Add a new event"))
		self.lSubject.setText(_translate("MainWindow", "Subject:"))
		self.ltags.setText(_translate("MainWindow", "tags:"))
		self.llevel.setText(_translate("MainWindow", "level:"))
		self.lcompletion.setText(_translate("MainWindow", "completion:"))
		self.ldue_date.setText(_translate("MainWindow", "due date:"))
		self.lcomment.setText(_translate("MainWindow", "comment:"))
		self.save.setText(_translate("MainWindow", "Save"))
		self.delete.setText(_translate("MainWindow", "Delete"))
		self.complete.setText(_translate("MainWindow", "Complete"))
		self.completion_percentage.setText(_translate("MainWindow", ""))

	def add_block(self, Block):
		block = QtWidgets.QFrame()
		grid = QtWidgets.QGridLayout(block)
		subject = QtWidgets.QLabel()
		subject.setText(Block.subject)
		# grid.addWidget(subject)
		progress = QtWidgets.QProgressBar()
		progress.setProperty("value", int(Block.completeness))
		edit = QtWidgets.QPushButton()
		edit.setText("edit")
		edit.clicked.connect(lambda: self.select_block(Block))

		grid.addWidget(subject, 0, 0)
		grid.addWidget(edit, 0, 1)
		grid.addWidget(progress, 1, 0)
		self.formLayout.addRow(block)

	def add_nothing_hint(self):
		label1 = QtWidgets.QLabel("There is nothing here now.")
		self.formLayout.addRow(label1)

	def clear_left_list(self):
		while(self.formLayout.count()):
			self.formLayout.takeAt(0).widget().deleteLater()

	def set_left_list(self, lst):
		self.clear_left_list()
		if len(lst) == 0:
			self.add_nothing_hint()
		else:
			for d in lst:
				self.add_block(d)


	def set_percentage(self):self.completion_percentage.setText(str(self.completion.value())+"%")

	def change_sorting(self, lst_):
		tag = self.tag_filter.currentText()
		sortby = self.sort_by.currentText()
		status = self.completeness.currentText()

		lst = []

		for block in lst_:
			if (tag in block.tags or tag == 'ALL') and (status == block.status or status == "default") and block.status != "deleted":
				lst.append(block)
		if sortby == "default":
			lst.sort(key = lambda d: d.creation_time, reverse = True)
		elif sortby == "due_date":
			lst.sort(key = lambda d: d.due_date)
		elif sortby == "completion":
			lst.sort(key = lambda d: int(d.completeness), reverse = True)

		self.set_left_list(lst)

	def select_block(self, block):
		self.now_selecting = block.number
		self.set_editing_interface(block)

	def set_editing_interface(self, block):
		self.Subject.setText(block.subject)
		self.tags.setText(' '.join(block.tags))
		if block.level == "Easy": self.level.setCurrentIndex(0)
		elif block.level == "Normal": self.level.setCurrentIndex(1)
		elif block.level == "Hard": self.level.setCurrentIndex(2)
		self.completion.setValue(int(block.completeness))
		self.due_date.setDateTime(QtCore.QDateTime.fromString(block.due_date, "yyyyMMddhhmmss"))
		self.comment.setText(block.comment)

	def save_block(self, maindata):
		block = maindata.blocks[self.now_selecting]
		if block.status != "deleted":
			block.subject = self.Subject.text()
			block.tags = self.tags.text().split(' ')
			for tag in block.tags:
				if tag not in maindata.tags:
					self.tag_filter.addItems([tag])
					maindata.tags.add(tag)
			block.level = self.level.currentText()
			if(block.status == "Completed"): block.completeness = "100"
			else:
				block.completeness = str(self.completion.value())
				if(block.completeness == "0"): block.status = "Not Started Yet"
				else: block.status = "In Progress"
			block.due_date = self.due_date.dateTime().toString("yyyyMMddhhmmss")
			block.comment = self.comment.toPlainText()
		self.refresh_left_list()
		self.set_editing_interface(block)
		maindata.save()

	def delete_qm(self, maindata):
		qm = QtWidgets.QMessageBox()
		qm.setText("Delete?")
		qm.setStandardButtons(qm.Yes | qm.No)
		ret = qm.exec()
		if ret == qm.Yes:
			self.delete_block(maindata)

	def delete_block(self, maindata):
		maindata.blocks[self.now_selecting].status = "deleted"
		self.refresh_left_list()
		self.select_block(maindata.blocks[0])
		maindata.save()


	def complete_block(self, maindata):
		block = maindata.blocks[self.now_selecting]
		block.status = "Completed"
		block.completeness = "100"
		self.refresh_left_list()
		maindata.save()

	def create_new_empty_block(self, maindata):
		empty_block = create_empty_block()
		empty_block.number = len(maindata.blocks)
		maindata.blocks.append(empty_block)
		block = maindata.blocks[-1]
		self.set_search_option_default()
		self.refresh_left_list()
		self.select_block(block)

	def refresh_left_list(self):
		index = self.completeness.currentIndex()
		if index == 0:
			self.completeness.setCurrentIndex(1)
			self.completeness.setCurrentIndex(index)
		else:
			self.completeness.setCurrentIndex(0)
			self.completeness.setCurrentIndex(index)

	def set_search_option_default(self):
		self.tag_filter.setCurrentIndex(0)
		self.sort_by.setCurrentIndex(0)
		self.completeness.setCurrentIndex(0)



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