import gradio as gr


class GPACalculator:
    def __init__(self):
        self.grade_tables = {
            "Tier III": {
                100: 6.0, 99: 5.9, 98: 5.8, 97: 5.7, 96: 5.6, 95: 5.5, 94: 5.4, 93: 5.3, 92: 5.2, 91: 5.1,
                90: 5.0, 89: 4.9, 88: 4.8, 87: 4.7, 86: 4.6, 85: 4.5, 84: 4.4, 83: 4.3, 82: 4.2, 81: 4.1,
                80: 4.0, 79: 3.9, 78: 3.8, 77: 3.7, 76: 3.6, 75: 3.5, 74: 3.4, 73: 3.3, 72: 3.2, 71: 3.1,
                70: 3.0, 'Below 70': 0
            },
            "Tier II": {
                100: 5.5, 99: 5.4, 98: 5.3, 97: 5.2, 96: 5.1, 95: 5.0, 94: 4.9, 93: 4.8, 92: 4.7, 91: 4.6,
                90: 4.5, 89: 4.4, 88: 4.3, 87: 4.2, 86: 4.1, 85: 4.0, 84: 3.9, 83: 3.8, 82: 3.7, 81: 3.6,
                80: 3.5, 79: 3.4, 78: 3.3, 77: 3.2, 76: 3.1, 75: 3.0, 74: 2.9, 73: 2.8, 72: 2.7, 71: 2.6,
                70: 2.5, 'Below 70': 0
            },
            "Tier I": {
                100: 5.0, 99: 4.9, 98: 4.8, 97: 4.7, 96: 4.6, 95: 4.5, 94: 4.4, 93: 4.3, 92: 4.2, 91: 4.1,
                90: 4.0, 89: 3.9, 88: 3.8, 87: 3.7, 86: 3.6, 85: 3.5, 84: 3.4, 83: 3.3, 82: 3.2, 81: 3.1,
                80: 3.0, 79: 2.9, 78: 2.8, 77: 2.7, 76: 2.6, 75: 2.5, 74: 2.4, 73: 2.3, 72: 2.2, 71: 2.1,
                70: 2.0, 'Below 70': 0
            }
        }

    def calculate_gpa(self, grades, previous_gpa):
    total_points = previous_gpa * sum(len(grade_list) for grade_list in grades.values())
    total_courses = sum(len(grade_list) for grade_list in grades.values())
    
    for tier, tier_grades in grades.items():
        if tier_grades:  # Check if there are grades in the tier
            for grade in tier_grades:
                total_points += self.grade_tables[tier].get(grade, 0)
                total_courses += 1
                
    if total_courses == 0:
        return 0  # Avoid division by zero
    return total_points / total_courses


calculator = GPACalculator()

def calculate_gpa_interface(grades_tier_1, grades_tier_2, grades_tier_3, previous_gpa):
    
        

        grades_tier_1 = [int(x.strip()) for x in grades_tier_1.split(",")]
        grades_tier_2 = [int(x.strip()) for x in grades_tier_2.split(",")]
        grades_tier_3 = [int(x.strip()) for x in grades_tier_3.split(",")]

        grades = {
            "Tier I": grades_tier_1,
            "Tier II": grades_tier_2,
            "Tier III": grades_tier_3
        }

        gpa = calculator.calculate_gpa(grades, float(previous_gpa))
        return str(gpa)


# Create Gradio interface
title = "GPA Calculator for FISD"
instructions = "Enter your grades for on-level, advanced, and AP courses, along with your previous GPA."
demo = gr.Interface(fn=calculate_gpa_interface, 
                     inputs=[gr.Textbox(label="On-level Grades", lines=2),
                             gr.Textbox(label="Advanced Grades"),
                             gr.Textbox(label="AP Grades"),
                             gr.Textbox(label="Previous GPA")], 
                     outputs=[gr.Textbox(label="Calculated GPA")],
                     title=title, 
                     description=instructions)

    
    

if __name__ == "__main__":
    demo.launch()