/*
 * Copyright (C) 2026 chatellier
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 */
package scripts;

import org.nuiton.topia.TopiaContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.io.FileUtils;

import fr.ifremer.isisfish.annotations.Doc;
import fr.ifremer.isisfish.IsisFishDAOHelper;
import fr.ifremer.isisfish.datastore.RegionStorage;
import fr.ifremer.isisfish.entities.*;
import org.nuiton.math.matrix.*;
import java.util.*;
import java.io.*;
import fr.ifremer.isisfish.datastore.RegionStorage;

/**
 * RenommageNoms.java
 */
public class RenommageNoms {

    /** to use log facility, just put in your code: log.info("..."); */
    private static Log log = LogFactory.getLog(RenommageNoms.class);

    public static File dossierCsv = new File("/home/chatellier/projets/ifremer-isisfish/userdata/sophie/");

    public static void main(String... args) {
        RenommageNoms script = new RenommageNoms();
        script.run();
        System.out.println("Done: " + new Date());
    }

    public void run() {
        // ouverture d'une region par son nom
        RegionStorage region = RegionStorage.getRegion("GEMMBE_140426");

        // ouverture d'une transaction pour effectuer toutes les modifications
        TopiaContext tx = region.getStorage().beginTransaction();

        try {
            // utilisation du contexte dans d'autre methodes
            renommageMetier(tx);
            renommageZone(tx);
            renommageStrategie(tx);
            renommageFlottille(tx);
    
            // commit : valide les transactions en base de données
            // roolback : annule les modifications en base de données (permet de tester le script)
            //tx.commitTransaction();
            tx.rollbackTransaction();
            
        } catch (Exception ex) {
            System.err.println("Erreur");
            ex.printStackTrace();
            tx.rollbackTransaction();
        } finally {
            // close region
            tx.closeContext();
        }
    }

    protected void renommageMetier(TopiaContext tx) throws IOException {
        File file = new File(dossierCsv, "correspondance_metier.csv");
        List<String> lines = FileUtils.readLines(file, "utf-8");
        lines.remove(0); // remove header

        MetierDAO metierDAO = IsisFishDAOHelper.getMetierDAO(tx);
        for (String line : lines) {
            String oldName = StringUtils.substringBefore(line, ";").trim();
            String newName = StringUtils.substringAfter(line, ";").trim();

            Metier obj = metierDAO.findByName(oldName);
            System.out.println("Rename m '" + oldName + "' to '" + newName + "' : " + obj);
            obj.setName(newName);
        }
    }

    protected void renommageZone(TopiaContext tx) throws IOException {
        File file = new File(dossierCsv, "correspondance_zone.csv");
        List<String> lines = FileUtils.readLines(file, "utf-8");
        lines.remove(0); // remove header

        ZoneDAO zoneDAO = IsisFishDAOHelper.getZoneDAO(tx);
        for (String line : lines) {
            String oldName = StringUtils.substringBefore(line, ";").trim();
            String newName = StringUtils.substringAfter(line, ";").trim();

            Zone obj = zoneDAO.findByName(oldName);
            System.out.println("Rename z '" + oldName + "' to '" + newName + "' : " + obj);
            obj.setName(newName);
        }
    }

    protected void renommageStrategie(TopiaContext tx) throws IOException {
        File file = new File(dossierCsv, "correspondance_str.csv");
        List<String> lines = FileUtils.readLines(file, "utf-8");
        lines.remove(0); // remove header

        StrategyDAO strDAO = IsisFishDAOHelper.getStrategyDAO(tx);
        for (String line : lines) {
            String oldName = StringUtils.substringBefore(line, ";").trim();
            String newName = StringUtils.substringAfter(line, ";").trim();

            Strategy obj = strDAO.findByName(oldName);
            if (obj == null) {
                System.err.println("########## Can't find str " + oldName);
            } else {
                System.out.println("Rename s '" + oldName + "' to '" + newName + "' : " + obj);
                obj.setName(newName);
            }
        }
    }

    protected void renommageFlottille(TopiaContext tx) throws IOException {
        File file = new File(dossierCsv, "correspondance_flo.csv");
        List<String> lines = FileUtils.readLines(file, "utf-8");
        lines.remove(0); // remove header

        SetOfVesselsDAO sovDAO = IsisFishDAOHelper.getSetOfVesselsDAO(tx);
        for (String line : lines) {
            String oldName = StringUtils.substringBefore(line, ";").trim();
            String newName = StringUtils.substringAfter(line, ";").trim();

            SetOfVessels obj = sovDAO.findByName(oldName);
            if (obj == null) {
                System.err.println("########## Can't find Flottille " + oldName);
            } else {
                System.out.println("Rename sv '" + oldName + "' to '" + newName + "' : " + obj);
                obj.setName(newName);
            }
        }
    }

}
