package com.sptg.repeater;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sptg.repeater.vo.CommonValues;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

@Slf4j
@SpringBootApplication
public class RepeaterApplication {

    String HOST = "127.0.0.1";
    String USERNAME = "guest";
    String PASSWORD = "guest";
    String VIRTUAL = "smart-house";
    int PORT = 5672;

    @Autowired
    private CommonValues cv;

    public static void main(String[] args) {
        SpringApplication.run(RepeaterApplication.class, args);
    }

    @Bean
    public ConnectionFactory connectionFactory() {

        String url = cv.getLzUrl() + "device/dh/status/start";
        JSONObject param = JSONUtil.createObj()
                .put("deviceFactory", "dh");
        // 请求mq连接参数
        String result = HttpRequest.post(url)
                .header("data-sign", "471e6f4ed225a95664d7a5a9ae07cd4c")
                .body(param.toString())
                .execute().body();

        if(!StrUtil.isBlank(result)){
            JSONObject res = JSONUtil.parseObj(result);
            JSONObject data = JSONUtil.parseObj(res.getStr("data"));
            HOST = data.getStr("host");
            PORT = data.getInt("port");
            USERNAME = data.getStr("user");
            PASSWORD = data.getStr("password");
            VIRTUAL = data.getStr("virtual");
            log.warn(result);
//            System.out.println(result);
        }


        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();

        System.err.println(HOST + "==="+ PORT + "==="+ USERNAME+ "==="+ PASSWORD+ "==="+ VIRTUAL);

        connectionFactory.setHost(HOST);
        connectionFactory.setPort(PORT);
        connectionFactory.setUsername(USERNAME);
        connectionFactory.setPassword(PASSWORD);
        connectionFactory.setVirtualHost(VIRTUAL);
        //必须要设置,消息的回掉
        connectionFactory.setPublisherConfirms(true);

        return connectionFactory;
    }

    /**
     * 设置自定义yml位置
     *
     * @return
     */
//    @Bean
//    public static PropertySourcesPlaceholderConfigurer properties() {
//        PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
//        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
//        yaml.setResources(
//                new ClassPathResource[] {
//                        /** 请求url地址 */
//                        new ClassPathResource("config/commonValues.yml")
//                });
//        pspc.setProperties(yaml.getObject());
//        return pspc;
//    }
}
