package datamanagement;

public class CheckGradeController
{
  // ===========================================================================
  // Variables
  // ===========================================================================
  
  
  
  private CheckGradeUserInterface userInterface_;
  private String currentUnitCode_ = null;
  private Integer currentStudentId_ = null;
  private boolean isChangesMade_ = false;

  

  // ===========================================================================
  // Constructors
  // ===========================================================================
  
  
  
  public CheckGradeController()
  {
  }


  
  // ===========================================================================
  // Methods
  // ===========================================================================
  
  
  
  public void initialize()
  {
    userInterface_ = new CheckGradeUserInterface(this);
    ListUnitsCTL listUnitsController = new ListUnitsCTL();
    
    userInterface_.setSelectUnitComboBoxEnabled(false);
    userInterface_.setSelectStudentComboBoxEnabled(false);
    userInterface_.setCheckGradeButtonEnabled(false);
    userInterface_.setChangeButtonEnabled(false);
    userInterface_.setMarkEntryTextFieldsEnabled(false);
    userInterface_.setSaveButtonEnabled(false);
    userInterface_.clearDisplayedText();

    listUnitsController.listUnits(userInterface_);
    userInterface_.setVisible(true);
    userInterface_.setSelectUnitComboBoxEnabled(true);
  }



  public void unitSelectedHandler(String unitCode)
  {

    if (unitCode.equals("NONE")) {
      userInterface_.setSelectStudentComboBoxEnabled(false);
    }
    else {
      ListStudentsCTL listStudentsController = new ListStudentsCTL();
      
      listStudentsController.listStudents(userInterface_, unitCode);
      currentUnitCode_ = unitCode;
      userInterface_.setSelectStudentComboBoxEnabled(true);
    }
    userInterface_.setCheckGradeButtonEnabled(false);
  }



  public void studentSelectedHandler(Integer studentId)
  {
    currentStudentId_ = studentId;
    
    if (currentStudentId_.intValue() == 0) {
      userInterface_.clearDisplayedText();
      userInterface_.setCheckGradeButtonEnabled(false);
      userInterface_.setChangeButtonEnabled(false);
      userInterface_.setMarkEntryTextFieldsEnabled(false);
      userInterface_.setSaveButtonEnabled(false);
    }

    else {
      IStudent student = StudentManager.get().getStudent(studentId);
      IStudentUnitRecord studentUnitRecord = student
                                             .getUnitRecord(currentUnitCode_);

      userInterface_.setStudentUnitRecord(studentUnitRecord);
      userInterface_.setCheckGradeButtonEnabled(true);
      userInterface_.setChangeButtonEnabled(true);
      userInterface_.setMarkEntryTextFieldsEnabled(false);
      userInterface_.setSaveButtonEnabled(false);

      isChangesMade_ = false;
    }
  }



  public String checkGradeHandler(float markForAssignmentOne,
                                  float markForAssignmentTwo, float markForExam)
  {
    IUnit unit = UnitManager.getInstance().getUnit(currentUnitCode_);
    String grade = unit.calculateGrade(markForAssignmentOne,
                                       markForAssignmentTwo, markForExam);

    userInterface_.setChangeButtonEnabled(true);
    userInterface_.setMarkEntryTextFieldsEnabled(false);

    if (isChangesMade_) {
      userInterface_.setSaveButtonEnabled(true);
    }
    return grade;
  }


  
  public void saveGradeHandler(float markForAssignmentOne, 
                               float markForAssignmentTwo, float markForExam)
  {

    IUnit unit = UnitManager.getInstance().getUnit(currentUnitCode_);
    IStudent student = StudentManager.get().getStudent(currentStudentId_);
    IStudentUnitRecord studentUnitrecord = student
                                           .getUnitRecord(currentUnitCode_);

    studentUnitrecord.setMarkForAssignmentOne(markForAssignmentOne);
    studentUnitrecord.setMarkForAssignmentTwo(markForAssignmentTwo);
    studentUnitrecord.setMarkForExam(markForExam);

    StudentUnitRecordManager.getInstance().saveStudentUnitRecord(studentUnitrecord);
    userInterface_.setChangeButtonEnabled(true);
    userInterface_.setMarkEntryTextFieldsEnabled(false);
    userInterface_.setSaveButtonEnabled(false);
  }
  
  
  
  public void enableChangeMarks()
  {
    userInterface_.setChangeButtonEnabled(false);
    userInterface_.setSaveButtonEnabled(false);
    userInterface_.setMarkEntryTextFieldsEnabled(true);
    
    isChangesMade_ = true;
  }
}

@@ -980,18 +980,25 @@
 stUnitsC
-TL
+ontroller
  listUni
@@ -1026,18 +1026,25 @@
 stUnitsC
-TL
+ontroller
 ();%0D%0A   
@@ -1444,24 +1444,27 @@
 troller.list
+All
 Units(userIn
@@ -3969,24 +3969,16 @@
 Unit
-r
+R
 ecord =
- student
 %0D%0A  
@@ -3987,43 +3987,15 @@
     
-                                   
+student
 .get
@@ -4034,33 +4034,33 @@
 %0A    studentUnit
-r
+R
 ecord.setMarkFor
@@ -4104,33 +4104,33 @@
 %0A    studentUnit
-r
+R
 ecord.setMarkFor
@@ -4178,25 +4178,25 @@
  studentUnit
-r
+R
 ecord.setMar
@@ -4263,16 +4263,46 @@
 stance()
+%0D%0A                            
 .saveStu
@@ -4327,17 +4327,17 @@
 dentUnit
-r
+R
 ecord);%0D
@@ -4747,11 +4747,13 @@
 ;%0D%0A  %7D%0D%0A
+%0D%0A
 %7D%0D%0A

rename call to LUC & listAllUnits() in CGC & fmt