package _VisualDVM.ProjectData.Files;
import Common.CommonConstants;
import Common.Database.Objects.DBObject;
import Common.Utils.Utils_;
import _VisualDVM.Constants;
import _VisualDVM.ProjectData.LanguageName;

import java.io.File;
public abstract class projectFile_ extends DBObject {
    public FileType fileType = FileType.none;
    public LanguageName languageName = LanguageName.n;
    public LanguageStyle style = LanguageStyle.none;
    public projectFile_() {
    }
    public projectFile_(File file_in) {
        AutoDetectProperties(file_in.getName());
    }
    @Override
    public void SynchronizeFields(DBObject src) {
        super.SynchronizeFields(src);
        projectFile_ src_ = (projectFile_) src;
        fileType = src_.fileType;
        languageName = src_.languageName;
        style = src_.style;
    }
    public void AutoDetectProperties(String name_in) {
        //проверка запретных имен.
        String[] forbiddenNames = new String[]{
                CommonConstants.INTERRUPT,
                Constants.launch_script_name,
                Constants.default_binary_name,
                //--
                CommonConstants.DONE,
                CommonConstants.TIMEOUT,
                Constants.out_file,
                Constants.err_file,
                Constants.time_file,
                "Makefile"
        };
        for (String forbidden : forbiddenNames) {
            if (name_in.equalsIgnoreCase(forbidden)) {
                fileType = FileType.forbidden;
                return;
            }
        }
        //-
        switch (Utils_.getExtensionFromName(name_in)) {
            case "f":
            case "fdv":
            case "for":
            case "f77":
                fileType = FileType.program;
                languageName = LanguageName.fortran;
                style = LanguageStyle.fixed;
                break;
            case "f90":
                fileType = FileType.program;
                languageName = LanguageName.fortran;
                style = LanguageStyle.free;
                break;
            case "c":
            case "cdv":
                fileType = FileType.program;
                languageName = LanguageName.c;
                break;
            case "cpp":
                fileType = FileType.program;
                languageName = LanguageName.cpp;
                break;
            case "h":
                fileType = FileType.header;
                break;
            case "fh":
                fileType = FileType.header;
                languageName = LanguageName.fortran;
                break;
            case "o":
            case "gcda":
            case "gcno":
            case "gcov":
            case "exe":
            case "pdf":
                fileType = FileType.forbidden;
                break;
            case "":
                if (Utils_.isIntegerValue(name_in)) {
                    fileType = FileType.forbidden;
                } else {
                    fileType = FileType.none;
                    languageName = LanguageName.n;
                }
                break;
            default:
                fileType = FileType.none;
                languageName = LanguageName.n;
                break;
        }
    }
    //-
    public String getStyleOptions() {
        if (languageName == LanguageName.fortran) {
            switch (style) {
                case fixed:
                case extended:
                    return "-FI";
                case free:
                    return "-f90";
                case none:
                    break;
            }
        }
        return "";
    }
}
