from PyQt5 import QtCore, QtGui, QtWidgets, Qt
from backend import getProduct,getAvgRatingOf,getRatingOf

class DetailProduk(object):

    def __init__(self, id):
        self.data = getProduct(id)
        self.valid_produk = (len(self.data) != 0)
        self.rating = getRatingOf(id)
        self.avg_rating = getAvgRatingOf(id)
        self.pushButton_2 = QtWidgets.QPushButton()
        
    def setupUi(self, ScrollArea):
        if (self.valid_produk):
            ScrollArea.setObjectName("ScrollArea")
            ScrollArea.resize(1028, 635)
            sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Minimum)
            sizePolicy.setHorizontalStretch(0)
            sizePolicy.setVerticalStretch(0)
            sizePolicy.setHeightForWidth(ScrollArea.sizePolicy().hasHeightForWidth())
            ScrollArea.setSizePolicy(sizePolicy)
            ScrollArea.setSizeIncrement(QtCore.QSize(0, 0))
            ScrollArea.setStyleSheet("background-color:#FCF0CC;border-radius:15px;")
            ScrollArea.setWidgetResizable(True)
            self.scrollAreaWidgetContents = QtWidgets.QWidget()
            self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 1026, 633))
            sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
            sizePolicy.setHorizontalStretch(0)
            sizePolicy.setVerticalStretch(0)
            sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents.sizePolicy().hasHeightForWidth())
            self.scrollAreaWidgetContents.setSizePolicy(sizePolicy)
            self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
            self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
            self.verticalLayout_2.setObjectName("verticalLayout_2")
            self.verticalLayout = QtWidgets.QVBoxLayout()
            self.verticalLayout.setContentsMargins(15, 0, 15, 20)
            self.verticalLayout.setSpacing(10)
            self.verticalLayout.setObjectName("verticalLayout")
            self.header = QtWidgets.QLabel(self.scrollAreaWidgetContents)
            sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
            sizePolicy.setHorizontalStretch(0)
            sizePolicy.setVerticalStretch(0)
            sizePolicy.setHeightForWidth(self.header.sizePolicy().hasHeightForWidth())
            self.header.setSizePolicy(sizePolicy)
            self.header.setMinimumSize(QtCore.QSize(0, 55))
            self.header.setMaximumSize(QtCore.QSize(16777215, 55))
            self.header.setStyleSheet("font-size:20px;color:#861657;font:Playfair Display;")
            self.header.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
            self.header.setObjectName("header")
            self.verticalLayout.addWidget(self.header)
            self.Produk = QtWidgets.QWidget(self.scrollAreaWidgetContents)
            self.Produk.setMaximumSize(QtCore.QSize(1100, 250))
            self.Produk.setStyleSheet("background-color:#FFFFFF;margin : 0 10px;border-radius:20px;")
            self.Produk.setObjectName("Produk")
            self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.Produk)
            self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
            self.horizontalLayout_2.setObjectName("horizontalLayout_2")
            self.horizontalLayout = QtWidgets.QHBoxLayout()
            self.horizontalLayout.setContentsMargins(0, -1, 0, 0)
            self.horizontalLayout.setObjectName("horizontalLayout")
            self.image = QtWidgets.QLabel(self.Produk)
            sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
            sizePolicy.setHorizontalStretch(0)
            sizePolicy.setVerticalStretch(0)
            sizePolicy.setHeightForWidth(self.image.sizePolicy().hasHeightForWidth())
            self.image.setSizePolicy(sizePolicy)
            self.image.setMaximumSize(QtCore.QSize(220, 16777215))
            self.image.setStyleSheet("border-radius:20px;")
            self.image.setText("")
            pixmap = QtGui.QPixmap()
            pixmap.loadFromData(self.data[0][9])
            self.image.setPixmap(pixmap)
            self.image.setObjectName("image")
            self.horizontalLayout.addWidget(self.image)
            self.desc = QtWidgets.QWidget(self.Produk)
            self.desc.setObjectName("desc")
            self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.desc)
            self.verticalLayout_4.setContentsMargins(15, 0, -1, -1)
            self.verticalLayout_4.setObjectName("verticalLayout_4")
            self.verticalLayout_3 = QtWidgets.QVBoxLayout()
            self.verticalLayout_3.setObjectName("verticalLayout_3")
            self.widget = QtWidgets.QWidget(self.desc)
            self.widget.setObjectName("widget")
            self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.widget)
            self.horizontalLayout_4.setContentsMargins(0, -1, -1, 0)
            self.horizontalLayout_4.setObjectName("horizontalLayout_4")
            self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
            self.horizontalLayout_3.setObjectName("horizontalLayout_3")
            self.product_name = QtWidgets.QLabel(self.widget)
            self.product_name.setStyleSheet("color:#861657;font-size:22px;font:Playfair Display;")
            self.product_name.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
            self.product_name.setObjectName("product_name")
            self.horizontalLayout_3.addWidget(self.product_name)
            self.price = QtWidgets.QLabel(self.widget)
            self.price.setStyleSheet("color:#861657;font-size:22px;font:Playfair Display;")
            self.price.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTop|QtCore.Qt.AlignTrailing)
            self.price.setObjectName("price")
            self.horizontalLayout_3.addWidget(self.price)
            self.horizontalLayout_4.addLayout(self.horizontalLayout_3)
            self.verticalLayout_3.addWidget(self.widget)
            self.label = QtWidgets.QLabel(self.desc)
            self.label.setMaximumSize(QtCore.QSize(600, 28))
            self.label.setStyleSheet("color:#861657;font-size:12px;font:Playfair Display;")
            self.label.setObjectName("label")
            self.verticalLayout_3.addWidget(self.label)
            self.label_2 = QtWidgets.QLabel(self.desc)
            self.label_2.setStyleSheet("color:#861657;font-size:12px;font:Playfair Display;")
            self.label_2.setObjectName("label_2")
            self.verticalLayout_3.addWidget(self.label_2)
            self.label_3 = QtWidgets.QLabel(self.desc)
            self.label_3.setStyleSheet("color:#861657;font-size:12px;font:Playfair Display;")
            self.label_3.setObjectName("label_3")
            self.verticalLayout_3.addWidget(self.label_3)
            self.widget_2 = QtWidgets.QWidget(self.desc)
            self.widget_2.setObjectName("widget_2")
            self.horizontalLayout_7 = QtWidgets.QHBoxLayout(self.widget_2)
            self.horizontalLayout_7.setContentsMargins(0, 5, 0, 5)
            self.horizontalLayout_7.setSpacing(10)
            self.horizontalLayout_7.setObjectName("horizontalLayout_7")
            self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
            self.horizontalLayout_6.setSpacing(15)
            self.horizontalLayout_6.setObjectName("horizontalLayout_6")
            self.pushButton_2 = QtWidgets.QPushButton(self.widget_2)
            self.pushButton_2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
            sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Fixed)
            sizePolicy.setHorizontalStretch(0)
            sizePolicy.setVerticalStretch(0)
            sizePolicy.setHeightForWidth(self.pushButton_2.sizePolicy().hasHeightForWidth())
            self.pushButton_2.setSizePolicy(sizePolicy)
            self.pushButton_2.setMinimumSize(QtCore.QSize(0, 40))
            self.pushButton_2.setMaximumSize(QtCore.QSize(250, 16777215))
            self.pushButton_2.setStyleSheet("QPushButton{background-color:#A64253;color:#FCF0CC;border-radius:10px;}\n"
            "QPushButton:hover{background-color:#861657;}")
            self.pushButton_2.setObjectName("pushButton_2")
            self.horizontalLayout_6.addWidget(self.pushButton_2)
            self.pushButton = QtWidgets.QPushButton(self.widget_2)
            self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
            sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Fixed)
            sizePolicy.setHorizontalStretch(0)
            sizePolicy.setVerticalStretch(0)
            sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
            self.pushButton.setSizePolicy(sizePolicy)
            self.pushButton.setMinimumSize(QtCore.QSize(0, 40))
            self.pushButton.setMaximumSize(QtCore.QSize(250, 16777215))
            self.pushButton.setStyleSheet("QPushButton{background-color:#A64253;color:#FCF0CC;border-radius:10px;}\n"
            "QPushButton:hover{background-color:#861657;}")
            self.pushButton.setObjectName("pushButton")
            self.horizontalLayout_6.addWidget(self.pushButton)
            self.horizontalLayout_7.addLayout(self.horizontalLayout_6)
            self.verticalLayout_3.addWidget(self.widget_2)
            self.verticalLayout_4.addLayout(self.verticalLayout_3)
            self.horizontalLayout.addWidget(self.desc)
            self.horizontalLayout_2.addLayout(self.horizontalLayout)
            self.verticalLayout.addWidget(self.Produk)
            self.header2 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
            sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Fixed)
            sizePolicy.setHorizontalStretch(0)
            sizePolicy.setVerticalStretch(0)
            sizePolicy.setHeightForWidth(self.header.sizePolicy().hasHeightForWidth())
            self.header2.setSizePolicy(sizePolicy)
            self.header2.setMinimumSize(QtCore.QSize(0, 55))
            self.header2.setMaximumSize(QtCore.QSize(16777215, 55))
            self.header2.setStyleSheet("font-size:20px;color:#861657;font:Playfair Display;")
            self.header2.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
            self.header2.setObjectName("header2")
            self.verticalLayout.addWidget(self.header2)

            for i in range (len(self.rating)):
                self.verticalWidget = QtWidgets.QWidget(self.scrollAreaWidgetContents)
                self.verticalWidget.setMinimumSize(QtCore.QSize(500, 120))
                self.verticalWidget.setStyleSheet("font-size:12px;color:#861657;font:Playfair Display;background-color:white;border-radius:10px;")
                self.verticalWidget.setObjectName("verticalWidget")
                self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.verticalWidget)
                self.verticalLayout_5.setContentsMargins(20, 15, 15, -1)
                self.verticalLayout_5.setObjectName("verticalLayout_5")
                self.NamaUser = QtWidgets.QLabel(self.verticalWidget)
                self.NamaUser.setMaximumSize(QtCore.QSize(16777215, 25))
                self.NamaUser.setCursor(QtGui.QCursor(QtCore.Qt.IBeamCursor))
                self.NamaUser.setStyleSheet("font-size:20px;")
                self.NamaUser.setObjectName("NamaUser")
                self.verticalLayout_5.addWidget(self.NamaUser)
                self.Rating = QtWidgets.QLabel(self.verticalWidget)
                self.Rating.setMaximumSize(QtCore.QSize(16777215, 14))
                self.Rating.setObjectName("Rating")
                self.verticalLayout_5.addWidget(self.Rating)
                self.Testimoni = QtWidgets.QLabel(self.verticalWidget)
                self.Testimoni.setMaximumSize(QtCore.QSize(16777215, 80))
                self.Testimoni.setObjectName("Testimoni")
                self.NamaUser.setText(self.rating[i][0])
                self.Rating.setText(("Rating :" + str(self.rating[i][1])))
                self.Testimoni.setText(self.rating[i][2])
                self.verticalLayout_5.addWidget(self.Testimoni)
                self.verticalLayout.addWidget(self.verticalWidget)
                self.widget_3 = QtWidgets.QWidget(self.scrollAreaWidgetContents)
                self.widget_3.setObjectName("widget_3")
                self.verticalLayout.addWidget(self.widget_3)

            self.verticalLayout_2.addLayout(self.verticalLayout)
            ScrollArea.setWidget(self.scrollAreaWidgetContents)

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

    def retranslateUi(self, ScrollArea):
        _translate = QtCore.QCoreApplication.translate
        ScrollArea.setWindowTitle(_translate("ScrollArea", "ScrollArea"))
        self.header.setText(_translate("ScrollArea", "Detail Produk"))
        # id_barang nama_barang, nama_toko, stok, panjang, lebar, tinggi, berat, harga, foto, 
        # deskripsi, alamat, masi belom rating
        namabarang = self.data[0][1]
        harga = "Rp"+str(self.data[0][8])
        toko = "Toko " + self.data[0][2]
        lokasi = self.data[0][11]
        desc = self.data[0][10]
        berat = str(self.data[0][7])
        panjang = str(self.data[0][4])
        lebar = str(self.data[0][5])
        tinggi = str(self.data[0][6])

        if (self.avg_rating != '-'):
            text_label = "Rating : " + str(round(self.avg_rating,1)) + "\n"+ toko +" | " + lokasi
        else:
            text_label = "Rating : Belum ada rating untuk produk ini" + "\n"+ toko +" | " + lokasi

        text_dimensi = "Berat : " + berat + " gram\n"+ "Dimensi : " + panjang + " cm x " + lebar + " cm x "+ tinggi + " cm"
        text_stok = "\nStok tersedia : " + str(self.data[0][3])
        text_dimensi_stok = text_dimensi + text_stok
        text_rating = "Rating : " +'...'+ "\n"
        
        self.product_name.setText(_translate("ScrollArea", namabarang))
        self.price.setText(_translate("ScrollArea", harga))
        self.label.setText(_translate("ScrollArea",text_label))
        self.label_2.setText(_translate("ScrollArea", desc))
        self.label_3.setText(_translate("ScrollArea", text_dimensi_stok))
        self.pushButton_2.setText(_translate("ScrollArea", "Masukkan ke Keranjang"))
        self.pushButton.setText(_translate("ScrollArea", "Chat Penjual"))
        self.header2.setText(_translate("ScrollArea", "Ulasan"))

