package net.geedge;

import cn.hutool.extra.spring.EnableSpringUtil;
import cn.hutool.log.Log;
import net.geedge.api.entity.EnvApiYml;
import net.geedge.common.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

import java.util.TimeZone;

@EnableSpringUtil
@SpringBootApplication
public class EnvApiApplication {

    private final static Log log = Log.get();

    public static void main(String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        SpringApplication.run(EnvApiApplication.class, args);
    }

    @Autowired
    private Environment environment;

    @Bean
    public EnvApiYml envProperties() {
        EnvApiYml apiYml = new EnvApiYml();

        EnvApiYml.Env envEntity = apiYml.new Env();
        envEntity.setRoot(environment.getProperty("env.root"));
        envEntity.setType(environment.getProperty("env.type"));
        envEntity.setPlatform(environment.getProperty("env.platform"));

        EnvApiYml.Adb adb = apiYml.new Adb();
        adb.setSerial(environment.getProperty("adb.serial"));
        adb.setHost(environment.getProperty("adb.host"));
        adb.setPort(environment.getProperty("adb.port", Integer.class));

        String droidvncDefaultConfig = T.FileUtil.readUtf8String(T.FileUtil.file(T.WebPathUtil.getRootPath(), "./lib/droidvnc-np-defaults.json"));
        Integer vncPort = T.JSONUtil.parseObj(droidvncDefaultConfig).getInt("port", 5900);
        adb.setVncPort(vncPort);

        EnvApiYml.Vnc vnc = apiYml.new Vnc();
        vnc.setHost(environment.getProperty("vnc.host"));
        vnc.setPort(environment.getProperty("vnc.port", Integer.class));

        apiYml.setEnv(envEntity);
        apiYml.setAdb(adb);
        apiYml.setVnc(vnc);

        log.info("[envProperties] [value: {}]", T.JSONUtil.toJsonStr(apiYml));
        return apiYml;
    }
}