package MainModule_;
import Common.Current_;
import Common.Database.Objects.iDBObject;
import Common.Database.VisualiserDatabase;
import Common.Passes.Pass;
import Common.Passes.PassCode_;
import Common.Utils.TextLog;
import Common.Utils.Utils_;
import Common.Utils.Vector_;
import Visual_DVM_2021.PassStats.PassStats;
import Visual_DVM_2021.Passes.PassCode;
import _VisualDVM.Global;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Vector;
//Основной объект визуализатора.
public abstract class MainModule_<D extends VisualiserDatabase> {
    D db;
    Class<D> db_class;
    //--
    LinkedHashMap<PassCode_, Pass> passes;
    LinkedHashMap<Current_, Object> objects;  //Current
    //--
    public D getDb() {
        return db;
    }

    private void createPasses(){
        passes= new LinkedHashMap<>();
        for (Object code: getPassCodesEnum().getEnumConstants()){
            try {
                Class<?> clazz = Class.forName("Visual_DVM_2021.Passes.All." + code.toString());
                Pass pass = ((Pass) clazz.newInstance());
                passes.put((PassCode_) code, pass);
            } catch (Exception ex) {
               ex.printStackTrace();
            }
        }
    }
    public void setPassesControlsVisible(boolean flag, PassCode_... codes_in) {
        for (PassCode_ code_in : codes_in)
            getPass(code_in).setControlsVisible(flag);
    }
    public MainModule_(Class<D> db_class_in) {
        objects = new LinkedHashMap<>();
        createPasses();
        try {
            db_class = db_class_in;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        //---
    }
    public abstract Class getPassCodesEnum();
    public void ActivateDB() throws Exception {
        db = db_class.newInstance();
        db.Connect();
        db.CreateAllTables();
        db.prepareTablesStatements();
        db.Synchronize();
    }
    public void DeactivateDB() throws Exception{
        if (db != null) db.Disconnect();
    }
    //--
    public Object get(Current_ name) {
        if (!objects.containsKey(name))
            objects.put(name,null);
        return objects.get(name);
    }
    public Object set(Current_ name, Object object) {
        if (objects.containsKey(name))
            objects.replace(name, object);
        else objects.put(name, object);
        return object;
    }
    public boolean Check(TextLog Log, Current_... names) {
        for (Current_ name : names)
            if (get(name) == null)
                Log.Writeln_(name.getDescription() + " не выбран(а)");
        return Log.isEmpty();
    }
    //применять только для наследников iDBObject
    public boolean matchCurrentID(Current_ name, int id) {
        return (get(name) != null) && (((iDBObject) get(name)).id == id);
    }
    public Pass getPass(PassCode_ code){
        return passes.get(code);
    }
    public Vector<Pass> getFirstAccessPasses(){
        Vector<PassStats> sortedStats = new Vector<>(getDb().passStats.Data.values());
        System.out.println("sorted passes size= "+sortedStats.size());
        sortedStats.sort(new Comparator<PassStats>() {
            @Override
            public int compare(PassStats o1, PassStats o2) {
                return Integer.compare(o2.Usages, o1.Usages);
            }
        });
        Vector<Pass> res= new Vector<>();
        for (PassStats passStats: sortedStats){
            PassCode_ passCode = (PassCode_) Enum.valueOf(getPassCodesEnum(), passStats.code);
            res.add(getPass(passCode));
        }
        return res;
    }
}
