package com.rthoni.intellij.codefromds.dbo.options;

import com.intellij.database.model.DasTable;
import com.intellij.database.psi.DbDataSource;
import com.intellij.database.util.DasUtil;
import com.rthoni.intellij.codefromds.business.Helper;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import java.util.stream.Collectors;

/**
 * Created by robin on 11/15/16.
 */
public class GenerateOptions {

    private DataSourceSelection _selection;

    private String _modelsRelativePath;

    private String _dataSourceTemplateRelativePath;

    private String _modelsTemplateRelativePath;

    private String _configAbsolutePath;

    private String _filesExtension;

    public GenerateOptions(DbDataSource source) {
        _modelsRelativePath = "Models";
        _dataSourceTemplateRelativePath = "Templates" + File.separator + "DataSource.twig";
        _modelsTemplateRelativePath = "Templates" + File.separator + "Models.twig";
        _configAbsolutePath = source.getProject().getBasePath() + File.separator + "code-from-ds.json";
        _filesExtension = "cs";
        _selection = new DataSourceSelection(source);
    }

    public HashMap<String, Object> toMap()
    {
        HashMap<String, Object> map = new HashMap<>();

        map.put("modelsRelativePath", _modelsRelativePath);
        map.put("dataSourceTemplateRelativePath", _dataSourceTemplateRelativePath);
        map.put("modelsTemplateRelativePath", _modelsTemplateRelativePath);
        map.put("filesExtension", _filesExtension);
        map.put("selection", _selection == null ? null : new JSONObject(_selection.toMap()));

        return map;
    }

    public void fromJson(JSONObject json)
    {
        _modelsRelativePath = Helper.getJsonString(json, "modelsRelativePath");
        _dataSourceTemplateRelativePath = Helper.getJsonString(json, "dataSourceTemplateRelativePath");
        _modelsTemplateRelativePath = Helper.getJsonString(json, "modelsTemplateRelativePath");
        _filesExtension = Helper.getJsonString(json, "filesExtension");
        _selection.fromJson(json.getJSONObject("selection"));
    }

    public DataSourceSelection getSelection() {
        return _selection;
    }

    public void setSelection(DataSourceSelection selection) {
        _selection = selection;
    }

    public String getModelsRelativePath() {
        return _modelsRelativePath;
    }

    public void setModelsRelativePath(String modelsRelativePath) {
        _modelsRelativePath = modelsRelativePath;
    }

    public String getDataSourceTemplateRelativePath() {
        return _dataSourceTemplateRelativePath;
    }

    public void setDataSourceTemplateRelativePath(String dataSourceTemplateRelativePath) {
        _dataSourceTemplateRelativePath = dataSourceTemplateRelativePath;
    }

    public String getModelsTemplateRelativePath() {
        return _modelsTemplateRelativePath;
    }

    public void setModelsTemplateRelativePath(String modelsTemplateRelativePath) {
        _modelsTemplateRelativePath = modelsTemplateRelativePath;
    }

    public String getConfigAbsolutePath() {
        return _configAbsolutePath;
    }

    public void setConfigAbsolutePath(String configAbsolutePath) {
        _configAbsolutePath = configAbsolutePath;
    }

    public String getFilesExtension() {
        return _filesExtension;
    }

    public void setFilesExtension(String filesExtension) {
        _filesExtension = filesExtension;
    }
}
