package com.ruoyi.generator.service.front;

import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.generator.domain.GenProject;
import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.generator.domain.GenTableColumn;
import com.ruoyi.generator.domain.front.FrontProject;
import com.ruoyi.generator.domain.front.FrontProjectColumn;
import com.ruoyi.generator.domain.front.FrontProjectTable;
import com.ruoyi.generator.domain.front.dto.AppBlueprintDesign;
import com.ruoyi.generator.domain.front.dto.BusinessActionDesign;

public class FrontProjectConverter
{
    public static GenProject toGenProject(FrontProject project)
    {
        if (project == null)
        {
            return null;
        }
        GenProject genProject = new GenProject();
        genProject.setFrontDraft(true);
        genProject.setProjectId(project.getProjectId());
        genProject.setProjectName(project.getProjectName());
        genProject.setProjectFileName(project.getProjectFileName());
        genProject.setPackageName(project.getPackageName());
        genProject.setVersion(project.getVersion());
        genProject.setProjectDesc(project.getProjectDesc());
        genProject.setFrontFramework(project.getFrontFramework());
        genProject.setBackFramework(project.getBackFramework());
        genProject.setFrontendEnabled(project.getFrontendEnabled());
        genProject.setStatus(project.getStatus());
        genProject.setModules(project.getModules());
        genProject.setTables(toGenTables(project.getTables(), project.getPackageName()));
        AppBlueprintDesign appBlueprint = parseAppBlueprint(project.getAppBlueprint());
        genProject.setAppBlueprint(appBlueprint);
        genProject.setAppRoles(appBlueprint.getRoles());
        genProject.setFrontendMenus(appBlueprint.getFrontendMenus());
        genProject.setAdminMenus(appBlueprint.getAdminMenus());
        genProject.setBusinessActions(parseBusinessActions(project.getBusinessBlueprint()));
        return genProject;
    }

    private static AppBlueprintDesign parseAppBlueprint(String appBlueprint)
    {
        if (appBlueprint == null || appBlueprint.trim().length() == 0)
        {
            return new AppBlueprintDesign();
        }
        try
        {
            AppBlueprintDesign blueprint = JSON.parseObject(appBlueprint, AppBlueprintDesign.class);
            return blueprint == null ? new AppBlueprintDesign() : blueprint;
        }
        catch (RuntimeException e)
        {
            return new AppBlueprintDesign();
        }
    }

    private static List<BusinessActionDesign> parseBusinessActions(String businessBlueprint)
    {
        if (businessBlueprint == null || businessBlueprint.trim().length() == 0)
        {
            return new ArrayList<BusinessActionDesign>();
        }
        try
        {
            List<BusinessActionDesign> actions = JSON.parseArray(businessBlueprint, BusinessActionDesign.class);
            return actions == null ? new ArrayList<BusinessActionDesign>() : actions;
        }
        catch (RuntimeException e)
        {
            return new ArrayList<BusinessActionDesign>();
        }
    }

    private static List<GenTable> toGenTables(List<FrontProjectTable> tables, String packageName)
    {
        List<GenTable> genTables = new ArrayList<GenTable>();
        if (tables == null)
        {
            return genTables;
        }
        for (FrontProjectTable table : tables)
        {
            genTables.add(toGenTable(table, packageName));
        }
        return genTables;
    }

    private static GenTable toGenTable(FrontProjectTable table, String packageName)
    {
        GenTable genTable = new GenTable();
        genTable.setTableId(table.getTableId());
        genTable.setTableName(table.getTableName());
        genTable.setTableComment(table.getTableComment());
        genTable.setClassName(table.getClassName());
        genTable.setPackageName(packageName);
        genTable.setModuleName(table.getModuleName());
        genTable.setBusinessName(table.getBusinessName());
        genTable.setFunctionName(table.getFunctionName());
        genTable.setTplCategory(table.getTplCategory());
        genTable.setTplWebType(table.getTplWebType());
        genTable.setOptions(table.getOptions());
        genTable.setCreateTableSql(table.getCreateTableSql());
        List<GenTableColumn> columns = toGenColumns(table.getColumns());
        genTable.setColumns(columns);
        genTable.setPkColumn(findPkColumn(columns));
        return genTable;
    }

    private static GenTableColumn findPkColumn(List<GenTableColumn> columns)
    {
        for (GenTableColumn column : columns)
        {
            if ("1".equals(column.getIsPk()))
            {
                return column;
            }
        }
        return null;
    }

    private static List<GenTableColumn> toGenColumns(List<FrontProjectColumn> columns)
    {
        List<GenTableColumn> genColumns = new ArrayList<GenTableColumn>();
        if (columns == null)
        {
            return genColumns;
        }
        for (FrontProjectColumn column : columns)
        {
            GenTableColumn genColumn = new GenTableColumn();
            genColumn.setColumnId(column.getColumnId());
            genColumn.setTableId(column.getTableId());
            genColumn.setColumnName(column.getColumnName());
            genColumn.setColumnComment(column.getColumnComment());
            genColumn.setColumnType(column.getColumnType());
            genColumn.setJavaType(column.getJavaType());
            genColumn.setJavaField(column.getJavaField());
            genColumn.setIsPk(column.getIsPk());
            genColumn.setIsIncrement(column.getIsIncrement());
            genColumn.setIsRequired(column.getIsRequired());
            genColumn.setIsInsert(column.getIsInsert());
            genColumn.setIsEdit(column.getIsEdit());
            genColumn.setIsList(column.getIsList());
            genColumn.setIsQuery(column.getIsQuery());
            genColumn.setQueryType(column.getQueryType());
            genColumn.setHtmlType(column.getHtmlType());
            genColumn.setDictType(column.getDictType());
            genColumn.setSort(column.getSort());
            genColumns.add(genColumn);
        }
        return genColumns;
    }
}
