// Path: bxbot-ui-server-domain-objects/src/main/java/com/gazbert/bxbot/ui/server/domain/bot/BotConfig.java
// public class BotConfig {
// 
//     private String id;
//     private String alias;
//     private String baseUrl;
//     private String username;
//     private String password;
// 
//     // required for Jackson
//     public BotConfig() {
//     }
// 
//     public BotConfig(BotConfig other) {
//         this.id = other.id;
//         this.alias = other.alias;
//         this.baseUrl = other.baseUrl;
//         this.username = other.username;
//         this.password = other.password;
//     }
// 
//     public BotConfig(String id, String alias, String baseUrl, String username, String password) {
//         this.id = id;
//         this.alias = alias;
//         this.baseUrl = baseUrl;
//         this.username = username;
//         this.password = password;
//     }
// 
//     public String getId() {
//         return id;
//     }
// 
//     public void setId(String id) {
//         this.id = id;
//     }
// 
//     public String getAlias() {
//         return alias;
//     }
// 
//     public void setAlias(String alias) {
//         this.alias = alias;
//     }
// 
//     public String getBaseUrl() {
//         return baseUrl;
//     }
// 
//     public void setBaseUrl(String baseUrl) {
//         this.baseUrl = baseUrl;
//     }
// 
//     public String getUsername() {
//         return username;
//     }
// 
//     public void setUsername(String username) {
//         this.username = username;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     @Override
//     public String toString() {
//         return MoreObjects.toStringHelper(this)
//                 .add("id", id)
//                 .add("alias", alias)
//                 .add("baseUrl", baseUrl)
//                 .add("username", username)
//                 .add("password", password)
//                 .toString();
//     }
// }
// 
// Path: bxbot-ui-server-domain-objects/src/main/java/com/gazbert/bxbot/ui/server/domain/bot/BotStatus.java
// public class BotStatus {
// 
//     private String id;
//     private String name;
//     private String status;
// 
//     // required for Jackson
//     public BotStatus() {
//     }
// 
//     public BotStatus(BotStatus other) {
//         this.id = other.id;
//         this.name = other.name;
//         this.status = other.status;
//     }
// 
//     public BotStatus(String id, String alias, String status) {
//         this.id = id;
//         this.name = alias;
//         this.status = status;
//     }
// 
//     public String getId() {
//         return id;
//     }
// 
//     public void setId(String id) {
//         this.id = id;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public String getStatus() {
//         return status;
//     }
// 
//     public void setStatus(String status) {
//         this.status = status;
//     }
// 
//     @Override
//     public String toString() {
//         return MoreObjects.toStringHelper(this)
//                 .add("id", id)
//                 .add("name", name)
//                 .add("status", status)
//                 .toString();
//     }
// }
// 
// Path: bxbot-ui-server-repository/src/main/java/com/gazbert/bxbot/ui/server/repository/remote/runtime/BotStatusRepository.java
// public interface BotStatusRepository {
// 
//     BotStatus getBotStatus(BotConfig botConfig);
// }


import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestClientException;
import com.gazbert.bxbot.ui.server.domain.bot.BotConfig;
import com.gazbert.bxbot.ui.server.domain.bot.BotStatus;
import com.gazbert.bxbot.ui.server.repository.remote.runtime.BotStatusRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Gareth Jon Lynch
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.gazbert.bxbot.ui.server.repository.remote.runtime.impl;


/**
 * A REST client implementation of the remote Bot status repository.
 *
 * @author gazbert
 */
@Repository("botStatusRepository")
public class BotStatusRepositoryRestClient extends AbstractRuntimeRepositoryRestClient implements BotStatusRepository {

    private static final Logger LOG = LogManager.getLogger();
    private static final String STATUS_RESOURCE_PATH = RUNTIME_RESOURCE_PATH + "/status";

    public BotStatusRepositoryRestClient(RestTemplateBuilder restTemplateBuilder) {
        super(restTemplateBuilder);
    }

    @Override
    public BotStatus getBotStatus(BotConfig botConfig) {

        try {
            restTemplate.getInterceptors().clear();
            restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(
                    botConfig.getUsername(), botConfig.getPassword()));

            final String endpointUrl = botConfig.getBaseUrl() + STATUS_RESOURCE_PATH;
            LOG.info(() -> "Fetching BotStatus from: " + endpointUrl);

// Path: bxbot-ui-server-domain-objects/src/main/java/com/gazbert/bxbot/ui/server/domain/bot/BotConfig.java
// public class BotConfig {
// 
//     private String id;
//     private String alias;
//     private String baseUrl;
//     private String username;
//     private String password;
// 
//     // required for Jackson
//     public BotConfig() {
//     }
// 
//     public BotConfig(BotConfig other) {
//         this.id = other.id;
//         this.alias = other.alias;
//         this.baseUrl = other.baseUrl;
//         this.username = other.username;
//         this.password = other.password;
//     }
// 
//     public BotConfig(String id, String alias, String baseUrl, String username, String password) {
//         this.id = id;
//         this.alias = alias;
//         this.baseUrl = baseUrl;
//         this.username = username;
//         this.password = password;
//     }
// 
//     public String getId() {
//         return id;
//     }
// 
//     public void setId(String id) {
//         this.id = id;
//     }
// 
//     public String getAlias() {
//         return alias;
//     }
// 
//     public void setAlias(String alias) {
//         this.alias = alias;
//     }
// 
//     public String getBaseUrl() {
//         return baseUrl;
//     }
// 
//     public void setBaseUrl(String baseUrl) {
//         this.baseUrl = baseUrl;
//     }
// 
//     public String getUsername() {
//         return username;
//     }
// 
//     public void setUsername(String username) {
//         this.username = username;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     @Override
//     public String toString() {
//         return MoreObjects.toStringHelper(this)
//                 .add("id", id)
//                 .add("alias", alias)
//                 .add("baseUrl", baseUrl)
//                 .add("username", username)
//                 .add("password", password)
//                 .toString();
//     }
// }
// 
// Path: bxbot-ui-server-domain-objects/src/main/java/com/gazbert/bxbot/ui/server/domain/bot/BotStatus.java
// public class BotStatus {
// 
//     private String id;
//     private String name;
//     private String status;
// 
//     // required for Jackson
//     public BotStatus() {
//     }
// 
//     public BotStatus(BotStatus other) {
//         this.id = other.id;
//         this.name = other.name;
//         this.status = other.status;
//     }
// 
//     public BotStatus(String id, String alias, String status) {
//         this.id = id;
//         this.name = alias;
//         this.status = status;
//     }
// 
//     public String getId() {
//         return id;
//     }
// 
//     public void setId(String id) {
//         this.id = id;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public String getStatus() {
//         return status;
//     }
// 
//     public void setStatus(String status) {
//         this.status = status;
//     }
// 
//     @Override
//     public String toString() {
//         return MoreObjects.toStringHelper(this)
//                 .add("id", id)
//                 .add("name", name)
//                 .add("status", status)
//                 .toString();
//     }
// }
// 
// Path: bxbot-ui-server-repository/src/main/java/com/gazbert/bxbot/ui/server/repository/remote/runtime/BotStatusRepository.java
// public interface BotStatusRepository {
// 
//     BotStatus getBotStatus(BotConfig botConfig);
// }


// Path: bxbot-ui-server-repository/src/main/java/com/gazbert/bxbot/ui/server/repository/remote/runtime/impl/BotStatusRepositoryRestClient.java
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestClientException;
import com.gazbert.bxbot.ui.server.domain.bot.BotConfig;
import com.gazbert.bxbot.ui.server.domain.bot.BotStatus;
import com.gazbert.bxbot.ui.server.repository.remote.runtime.BotStatusRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Gareth Jon Lynch
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.gazbert.bxbot.ui.server.repository.remote.runtime.impl;


/**
 * A REST client implementation of the remote Bot status repository.
 *
 * @author gazbert
 */
@Repository("botStatusRepository")
public class BotStatusRepositoryRestClient extends AbstractRuntimeRepositoryRestClient implements BotStatusRepository {

    private static final Logger LOG = LogManager.getLogger();
    private static final String STATUS_RESOURCE_PATH = RUNTIME_RESOURCE_PATH + "/status";

    public BotStatusRepositoryRestClient(RestTemplateBuilder restTemplateBuilder) {
        super(restTemplateBuilder);
    }

    @Override
    public BotStatus getBotStatus(BotConfig botConfig) {

        try {
            restTemplate.getInterceptors().clear();
            restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(
                    botConfig.getUsername(), botConfig.getPassword()));

            final String endpointUrl = botConfig.getBaseUrl() + STATUS_RESOURCE_PATH;
            LOG.info(() -> "Fetching BotStatus from: " + endpointUrl);

            final BotStatus botStatus = restTemplate.getForObject(endpointUrl, BotStatus.class);