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

import com.intellij.database.psi.DbDataSource;
import com.rthoni.intellij.codefromds.business.Helper;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;

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

    private DataSourceSelection _selection;

    private String _dataSourceRelativePath;

    private String _modelsRelativePath;

    private String _dataSourceTemplateRelativePath;

    private String _modelsTemplateRelativePath;

    private String _configAbsolutePath;

    private String _filesExtension;

    private String _castFileRelativePath;

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

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

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

        return map;
    }

    public void fromJson(JSONObject json)
    {
        _dataSourceRelativePath = Helper.getJsonString(json, "dataSourceRelativePath");
        _modelsRelativePath = Helper.getJsonString(json, "modelsRelativePath");
        _dataSourceTemplateRelativePath = Helper.getJsonString(json, "dataSourceTemplateRelativePath");
        _modelsTemplateRelativePath = Helper.getJsonString(json, "modelsTemplateRelativePath");
        _filesExtension = Helper.getJsonString(json, "filesExtension");
        _castFileRelativePath = Helper.getJsonString(json, "castFileRelativePath");
        _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;
    }

    public String getCastFileRelativePath() {
        return _castFileRelativePath;
    }

    public void setCastFileRelativePath(String castFileRelativePath) {
        _castFileRelativePath = castFileRelativePath;
    }

    public String getDataSourceRelativePath() {
        return _dataSourceRelativePath;
    }

    public void setDataSourceRelativePath(String dataSourceRelativePath) {
        _dataSourceRelativePath = dataSourceRelativePath;
    }
}
