package org.tt.core.dm;

import org.tt.core.dm.convert.json.JSONConverter;
import org.tt.core.entity.datafetcher.Department;
import org.tt.core.entity.datafetcher.Group;
import org.tt.core.fetch.AbstractDataFetcher;
import org.tt.core.sql.AbstractQueries;
import org.tt.core.sql.AbstractSQLManager;
import org.tt.core.sql.H2Queries;
import org.tt.core.sql.ex.NoSuchDepartmentException;
import org.tt.core.sql.ex.NoSuchGroupException;

import java.sql.SQLException;
import java.util.Comparator;
import java.util.List;

public class UpdateManager extends SSUDataManager {
    
    public UpdateManager(AbstractSQLManager sqlm, AbstractQueries qrs, AbstractDataFetcher df, AbstractDataConverter dconv) {
            super(sqlm, qrs, df, dconv);
    }

    public void checkDepartments() throws SQLException, NoSuchDepartmentException, NoSuchGroupException {
        System.out.println("Checking departments for update!");

        List<Department> ssuDeps = df.getDepartments();
        List<Department> dbDeps = sqlm.getDepartments();
        
        for (Department d: dbDeps) {
                if (!(ssuDeps.contains(d))) {
                    System.out.println("Removed: "+d.getTag());
                    sqlm.deleteDepartment(d);
                }
            }

            for (Department d: ssuDeps) {
                if (!dbDeps.contains(d)) {
                    System.out.println("Added: " + d.getTag());
                    sqlm.putDepartment(d);
                    super.putDepartmentGroups(d.getTag()); //needed to fetch, won't use df directly
                    for (Group g : sqlm.getGroups(d.getTag())) { //represented in JSON format, needs conversion, accessing db
                        super.putTT(d.getTag(), g.getName()); //same is here
                    }
                }
            }

        for (Department sd: ssuDeps) {
            for (Department dbD: dbDeps) {
                if (dbD.getTag().equals(sd.getTag())&&
                        (dbD.getName().equals(sd.getName()))) {
                    if (!(dbD.getMessage().equals(sd.getMessage()))) {
                        System.out.println("Message updated for: "+sd.getTag());
                        sqlm.updateDepartmentMessage(dbD.getTag(), sd.getMessage()); //should not be accessible from ADM
                    }
                }

                if (dbD.getTag().equals(sd.getTag())&&(!dbD.getName().equals(sd.getName()))) {
                    System.out.println("Info updated for: "+ dbD.getTag());
                    sqlm.updateDepartmentInfo(sd.getName(), sd.getTag(), sd.getMessage(), dbD.getTag());
                }
            }
        }
    }

    public void checkGroups() throws SQLException, NoSuchDepartmentException, NoSuchGroupException {
        for(Department dep: sqlm.getDepartments()) {
            List<Group> ssuGroups = df.getGroups(dep.getTag());
            List<Group> dbGroups = sqlm.getGroups(dep.getTag());

            for (Group g: dbGroups) {
                if (!ssuGroups.contains(g)) {
                    System.out.println("Removed: "+g.getName()+" in "+dep.getTag());
                    sqlm.deleteGroupFromDepartment(dep, g);
                }
            }

            for (Group g: ssuGroups) {
                if (!dbGroups.contains(g)) {
                    System.out.println("Added: "+g.getName()+" in "+dep.getTag());
                        super.putTT(dep.getTag(), g.getName());
                    }
                }

        }
    }


}

@@ -163,16 +163,62 @@
 .Group;%0A
+import org.tt.core.entity.datafetcher.Lesson;%0A
 import o
@@ -474,16 +474,44 @@
 ption;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -3560,11 +3560,478 @@
 %0A    %7D%0A%0A
+    public void checkTimetables() throws SQLException, NoSuchDepartmentException, IOException, NoSuchGroupException %7B%0A        for (Department dep: sqlm.getDepartments()) %7B%0A            for (Group gr: sqlm.getGroups(dep.getTag())) %7B%0A                List%3CList%3CLesson%3E%3E ssuTT = df.getTT(dep.getTag(), gr.getName());%0A                List%3CList%3CLesson%3E%3E dbTT = sqlm.getLessonList(sqlm.getGroupID(dep.getTag(), gr.getName()));%0A                %0A            %7D%0A        %7D%0A    %7D%0A%0A
 %0A%7D%0A

update manager checkTT workarounds