import cv2
import csv
import cvzone
import time
from cvzone.HandTrackingModule import HandDetector


cap = cv2.VideoCapture(0)
cap.set(3, 1920)
cap.set(4, 1080)

detector = HandDetector(detectionCon=0.8)


class MCQ():
    def __init__(self, data):
        self.question = data[0]
        self.choice1 = data[1]
        self.choice2 = data[2]
        self.choice3 = data[3]
        self.choice4 = data[4]
        self.answer = int(data[5])

        self.userAns = None

    def update(self, cursor, bboxs):
        for x, bbox in enumerate(bboxs):
            x1, y1, x2, y2 = bbox
            if x1 < cursor[0] < x2 and y1 < cursor[1] < y2:
                self.userAns = x + 1
                cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), cv2.FILLED)


def rescale_frame(frame, percent=75):
    width = int(frame.shape[1] * percent / 100)
    height = int(frame.shape[0] * percent / 100)
    dim = (width, height)
    return cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)

# Import csv file data
pathCSV = "Mcqs.csv"
with open(pathCSV, newline='\n') as f:
    reader = csv.reader(f)
    dataAll = list(reader)[1:]

# Create object for each MCQ
mcqList = []
for q in dataAll:
    mcqList.append(MCQ(q))

print(len(mcqList))

qNo = 0
qTotal = len(dataAll)

while True:
    success, img = cap.read()
    img = cv2.flip(img, 1)
    hands, img = detector.findHands(img, flipType=False)

    if qNo < qTotal:
        mcq = mcqList[qNo]

        img, bbox = cvzone.putTextRect(img, mcq.question, [100, 100], 1, 1, offset=25, border=3)
        img, bbox1 = cvzone.putTextRect(img, mcq.choice1, [100, 250], 1, 1, offset=25, border=3)
        img, bbox2 = cvzone.putTextRect(img, mcq.choice2, [400, 250], 1, 1, offset=25, border=3)
        img, bbox3 = cvzone.putTextRect(img, mcq.choice3, [100, 350], 1, 1, offset=25, border=3)
        img, bbox4 = cvzone.putTextRect(img, mcq.choice4, [400, 350], 1, 1, offset=25, border=3)

        if hands:
            lmList = hands[0]['lmList']
            cursor = lmList[8]
            length, info = detector.findDistance(lmList[8], lmList[12])
            if length < 20:
                # print('Clicked')
                mcq.update(cursor, [bbox1, bbox2, bbox3, bbox4])

                if mcq.userAns is not None:
                    print(mcq.userAns)
                    time.sleep(0.3)
                    qNo += 1

# ////////// video : 38:57 ////////////

        # frame = rescale_frame(img, percent=100)
        # cv2.imshow('Virtual Quiz', frame)

        cv2.imshow("Img", img)
        cv2.waitKey(1)


