package com.purui.prims.headless.server.utils;

import com.alibaba.fastjson.JSONObject;
import com.purui.prims.headless.api.pojo.request.DatabaseReq;
import com.purui.prims.headless.api.pojo.response.DatabaseResp;
import com.purui.prims.headless.core.pojo.ConnectInfo;
import com.purui.prims.headless.core.pojo.Database;
import com.purui.prims.headless.server.persistence.dataobject.DatabaseDO;
import org.springframework.beans.BeanUtils;

public class DatabaseConverter {

    public static Database convert(DatabaseResp databaseResp) {
        Database database = new Database();
        BeanUtils.copyProperties(databaseResp, database);
        database.setVersion(databaseResp.getVersion());
        return database;
    }

    public static Database convert(DatabaseReq databaseReq) {
        Database database = new Database();
        BeanUtils.copyProperties(databaseReq, database);
        database.setVersion(databaseReq.getVersion());
        return database;
    }

    public static DatabaseDO convert(DatabaseReq databaseReq, DatabaseDO databaseDO) {
        BeanUtils.copyProperties(databaseReq, databaseDO);
        ConnectInfo connectInfo = getConnectInfo(databaseReq);
        databaseDO.setConfig(JSONObject.toJSONString(connectInfo));
        return databaseDO;
    }

    public static DatabaseResp convert(DatabaseDO databaseDO) {
        DatabaseResp databaseResp = new DatabaseResp();
        BeanUtils.copyProperties(databaseDO, databaseResp);
        ConnectInfo connectInfo = JSONObject.parseObject(databaseDO.getConfig(), ConnectInfo.class);
        databaseResp.setUrl(connectInfo.getUrl());
        databaseResp.setUsername(connectInfo.getUserName());
        databaseResp.setDatabase(connectInfo.getDatabase());
        return databaseResp;
    }

    public static DatabaseResp convertWithPassword(DatabaseDO databaseDO) {
        DatabaseResp databaseResp = convert(databaseDO);
        ConnectInfo connectInfo = JSONObject.parseObject(databaseDO.getConfig(), ConnectInfo.class);
        databaseResp.setPassword(connectInfo.getPassword());
        return databaseResp;
    }

    public static DatabaseDO convertDO(DatabaseReq databaseReq) {
        DatabaseDO databaseDO = new DatabaseDO();
        BeanUtils.copyProperties(databaseReq, databaseDO);
        ConnectInfo connectInfo = getConnectInfo(databaseReq);
        databaseDO.setConfig(JSONObject.toJSONString(connectInfo));
        return databaseDO;
    }

    public static ConnectInfo getConnectInfo(DatabaseResp databaseResp) {
        Database database = convert(databaseResp);
        ConnectInfo connectInfo = new ConnectInfo();
        connectInfo.setUserName(database.getUsername());
        connectInfo.setPassword(database.passwordDecrypt());
        connectInfo.setUrl(database.getUrl());
        connectInfo.setDatabase(database.getDatabase());
        return connectInfo;
    }

    public static ConnectInfo getConnectInfo(DatabaseReq databaseReq) {
        ConnectInfo connectInfo = new ConnectInfo();
        connectInfo.setUserName(databaseReq.getUsername());
        connectInfo.setPassword(databaseReq.getPassword());
        connectInfo.setUrl(databaseReq.getUrl());
        connectInfo.setDatabase(databaseReq.getDatabase());
        return connectInfo;
    }

}
