from PyQt5.QtWidgets import * 
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys 

class Country: 
	def __init__(self, country_name, population=0.0, area=0.0):
		self.country_name = country_name
		self.population = population
		self.area = area 
		self.setDensity() 

	def setDensity(self):
		if self.population != 0 and self.area != 0: 
			self.density = self.population / self.area 
		return self.density 

	def __repr__(self): 
		return f"{self.country_name}\t{self.population}\t{self.area}\t{self.density}"

	def __str__(self): 
		return "{}\t{}\t{:0.2f}\t{:0.2f}".format(
				self.country_name, self.population, self.area, self.density
			)


class CountryUi(QWidget):
	def __init__(self, parent =None, **options):
		super(CountryUi, self).__init__(parent, **options)
		self.setupUi()

	def setupUi(self):
		self.setWindowTitle("Country Population")
		self.setWindowIcon(QIcon("flower.png"))
		self.setGeometry(50, 50, 600, 550)

		self.createEntries()
		self.createList()
		vBox = QVBoxLayout()
		vBox.addWidget(self.entriesGroup)
		vBox.setSpacing(10)
		vBox.addWidget(self.listGroup)
		# vBox.addStretch()

		self.setLayout(vBox)

	def createEntries(self):
		self.entriesGroup = QGroupBox("Country Information", self)
		self.entriesGroup.setFont(QFont("Times", 14, 750))
		entriesGrid= QGridLayout()

		self.lblCountry = QLabel("Coutry name", self)
		self.lblPopulation = QLabel("Population", self)
		self.lblArea = QLabel("Area (km2)", self)
		

		self.entCountry = QLineEdit(self)
		self.entPopulation = QLineEdit(self)
		self.entArea = QLineEdit(self)
		

		self.btnRecord = QPushButton("Add Record", self)
		self.btnRecord.clicked.connect(self.addData)
		self.btnClear = QPushButton("Clear Data", self)
		self.btnClear.clicked.connect(self.clearData)
		self.btnQuit = QPushButton("Exit Program", self)
		self.btnQuit.clicked.connect(self.exitProgram)

		labels = [self.lblCountry, self.lblPopulation, self.lblArea]
		entries = [self.entCountry, self.entPopulation, self.entArea]
		buttons = [self.btnRecord, self.btnClear, self.btnQuit]

		for l, lbl in enumerate(labels):
			lbl.setFont(QFont("Times", 14, 750))
			lbl.setFixedSize(150, 40)
			entriesGrid.addWidget(lbl, l, 0, 1, 1)

		for e, ent in enumerate(entries):
			ent.setFont(QFont("Times", 14, 750))
			ent.setMinimumHeight(40)
			entriesGrid.addWidget(ent, e, 1, 1, 2)

		for b, btn in enumerate(buttons):
			btn.setFixedSize(150, 40)
			entriesGrid.addWidget(btn, 3, b, 1, 1)


		self.entriesGroup.setLayout(entriesGrid)

	def createList(self):
		self.listGroup = QGroupBox("List of Countries", self)
		self.listGroup.setFont(QFont("Times", 14, 750))

		hBox = QHBoxLayout()
		self.viewCountries = QListView(self)
		self.model = QStandardItemModel()
		self.viewCountries.setModel(self.model)
		self.viewCountries.setAlternatingRowColors(True)
		hBox.addWidget(self.viewCountries)

		self.listGroup.setLayout(hBox)

	def exitProgram(self):
		choice = QMessageBox.question(self, "Quit!", "Do you want to close the program?",
			QMessageBox.Yes|QMessageBox.No, QMessageBox.Yes)
		if choice==QMessageBox.Yes:
			self.close()

	def addData(self): 
		name = self.entCountry.text()
		pops = float(self.entPopulation.text())
		space = float(self.entArea.text())
		country = Country(name, pops, space)
		item = QStandardItem(country.__str__())
		self.model.appendRow(item)

	def clearData(self):
		self.entCountry.clear()
		self.entPopulation.clear()
		self.entArea.clear()

def main():
	app = QApplication(sys.argv)
	win = CountryUi()
	win.show()
	sys.exit(app.exec_())

if __name__ == '__main__':
	main()