package pt.utl.ist.scripts.runOnce.sites;

import java.util.Collection;

import net.sourceforge.fenixedu.domain.Department;
import net.sourceforge.fenixedu.domain.Section;
import net.sourceforge.fenixedu.domain.Site;
import net.sourceforge.fenixedu.domain.organizationalStructure.DepartmentUnit;
import pt.ist.bennu.core.domain.Bennu;
import pt.utl.ist.fenix.tools.util.i18n.Language;
import pt.utl.ist.fenix.tools.util.i18n.MultiLanguageString;
import pt.utl.ist.scripts.commons.AtomicScript;

public class UpdateDepartmentSiteSections extends AtomicScript {

    public static void main(String[] args) {
        processWriteTransaction(new UpdateDepartmentSiteSections());
        System.exit(0);
    }

    @Override
    protected void run() throws Exception {
        MultiLanguageString topSectionName = i18n("Topo", "Top");
        MultiLanguageString sideSectionName = i18n("Lateral", "Side");

        for (Department department : Bennu.getInstance().getDepartmentsSet()) {
            DepartmentUnit unit = department.getDepartmentUnit();

            if (unit == null) {
                continue;
            }

            System.out.print(unit.getNameWithAcronym() + ": ");

            Site site = unit.getSite();

            Section topSection = getTargetSection(site, topSectionName);
            if (topSection == null) {
                topSection = new Section(site, topSectionName);
                System.out.print("created-top ");
            }

            Section sideSection = getTargetSection(site, sideSectionName);
            if (sideSection == null) {
                sideSection = new Section(site, sideSectionName);
                System.out.print("created-side ");
            }

            Collection<Section> topSections = site.getOrderedTopLevelSections();
            topSections.remove(topSection);
            topSections.remove(sideSection);

            if (!topSections.isEmpty()) {
                System.out.print("moved-sections(" + topSections.size() + ")");
                for (Section section : topSections) {
                    section.setSuperiorSection(sideSection);
                }
            }

            System.out.println();
        }
    }

    private Section getTargetSection(Site site, MultiLanguageString name) {
        for (Section section : site.getTopLevelSections()) {
            if (equalInAnyLanguage(section.getName(), name)) {
                return section;
            }
        }

        return null;
    }

    protected boolean equalInAnyLanguage(MultiLanguageString target, MultiLanguageString sub) {
        for (Language language : sub.getAllLanguages()) {
            String text = sub.getContent(language);
            String targetText = target.getContent(language);

            if (targetText == null) {
                continue;
            }

            if (text.equalsIgnoreCase(targetText)) {
                return true;
            }
        }

        return false;
    }

    protected MultiLanguageString i18n(String pt, String en) {
        MultiLanguageString mls = new MultiLanguageString();
        mls.setContent(Language.pt, pt);
        mls.setContent(Language.en, en);

        return mls;
    }

}
