/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.jdbc.orchestration.api.datasource;

import com.google.common.base.Preconditions;
import com.google.common.eventbus.Subscribe;
import io.shardingsphere.core.api.ConfigMapContext;
import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingsphere.core.rule.MasterSlaveRule;
import io.shardingsphere.jdbc.orchestration.internal.OrchestrationFacade;
import io.shardingsphere.jdbc.orchestration.internal.circuit.datasource.CircuitBreakerDataSource;
import io.shardingsphere.jdbc.orchestration.internal.config.ConfigurationService;
import io.shardingsphere.jdbc.orchestration.internal.datasource.AbstractOrchestrationDataSource;
import io.shardingsphere.jdbc.orchestration.internal.event.config.MasterSlaveConfigurationEventBusEvent;
import io.shardingsphere.jdbc.orchestration.internal.event.state.DisabledStateEventBusEvent;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Orchestration master-slave datasource.
 *
 * @author panjuan
 */
@Slf4j
public class OrchestrationMasterSlaveDataSource extends AbstractOrchestrationDataSource {
    
    private MasterSlaveDataSource dataSource;
    
    public OrchestrationMasterSlaveDataSource(final MasterSlaveDataSource masterSlaveDataSource, final OrchestrationFacade orchestrationFacade) throws SQLException {
        super(orchestrationFacade, masterSlaveDataSource.getDataSourceMap());
        this.dataSource = masterSlaveDataSource;
        initOrchestrationFacade(masterSlaveDataSource);
    }
    
    public OrchestrationMasterSlaveDataSource(final OrchestrationFacade orchestrationFacade) throws SQLException {
        super(orchestrationFacade, orchestrationFacade.getConfigService().loadDataSourceMap());
        ConfigurationService configService = orchestrationFacade.getConfigService();
        MasterSlaveRuleConfiguration masterSlaveRuleConfig = configService.loadMasterSlaveRuleConfiguration();
        Preconditions.checkNotNull(masterSlaveRuleConfig, "Missing the master-slave rule configuration on register center");
        dataSource = new MasterSlaveDataSource(
                configService.loadDataSourceMap(), masterSlaveRuleConfig, configService.loadMasterSlaveConfigMap(), configService.loadMasterSlaveProperties());
    }
    
    private void initOrchestrationFacade(final MasterSlaveDataSource masterSlaveDataSource) {
        MasterSlaveRule masterSlaveRule = masterSlaveDataSource.getMasterSlaveRule();
        MasterSlaveRuleConfiguration masterSlaveRuleConfiguration = new MasterSlaveRuleConfiguration(
                masterSlaveRule.getName(), masterSlaveRule.getMasterDataSourceName(), masterSlaveRule.getSlaveDataSourceNames(), masterSlaveRule.getLoadBalanceAlgorithm());
        getOrchestrationFacade().init(masterSlaveDataSource.getDataSourceMap(), 
                masterSlaveRuleConfiguration, ConfigMapContext.getInstance().getMasterSlaveConfig(), masterSlaveDataSource.getShardingProperties().getProps());
    }
    
    @Override
    public final Connection getConnection() {
        if (isCircuitBreak()) {
            return new CircuitBreakerDataSource().getConnection();
        }
        return dataSource.getConnection();
    }
    
    @Override
    public final void close() {
        dataSource.close();
        getOrchestrationFacade().close();
    }
    
    /**
     * Renew master-slave data source.
     *
     * @param masterSlaveEvent master slave configuration event bus event
     * @throws SQLException sql exception
     */
    @Subscribe
    public void renew(final MasterSlaveConfigurationEventBusEvent masterSlaveEvent) throws SQLException {
        dataSource.close();
        dataSource = new MasterSlaveDataSource(
                masterSlaveEvent.getDataSourceMap(), masterSlaveEvent.getMasterSlaveRuleConfig(), ConfigMapContext.getInstance().getMasterSlaveConfig(), masterSlaveEvent.getProps());
    }
    
    /**
     * Renew disable dataSource names.
     *
     * @param disabledStateEventBusEvent jdbc disabled event bus event
     * @throws SQLException sql exception
     */
    @Subscribe
    public void renew(final DisabledStateEventBusEvent disabledStateEventBusEvent) throws SQLException {
        Map<String, DataSource> newDataSourceMap = getAvailableDataSourceMap(disabledStateEventBusEvent.getDisabledDataSourceNames());
        dataSource = new MasterSlaveDataSource(newDataSourceMap, dataSource.getMasterSlaveRule(), new LinkedHashMap<String, Object>(), dataSource.getShardingProperties());
    }
}

@@ -1014,16 +1014,95 @@
 veRule;%0A
+import io.shardingsphere.jdbc.orchestration.config.OrchestrationConfiguration;%0A
 import i
@@ -2152,38 +2152,45 @@
 al Orchestration
-Facade
+Configuration
  orchestrationFa
@@ -2179,38 +2179,38 @@
 on orchestration
-Facade
+Config
 ) throws SQLExce
@@ -2223,33 +2223,37 @@
 %7B%0A        super(
-o
+new O
 rchestrationFaca
@@ -2254,16 +2254,37 @@
 onFacade
+(orchestrationConfig)
 , master
@@ -2501,22 +2501,29 @@
 stration
-Facade
+Configuration
  orchest
@@ -2528,22 +2528,22 @@
 stration
-Facade
+Config
 ) throws
@@ -2572,17 +2572,21 @@
   super(
-o
+new O
 rchestra
@@ -2595,18 +2595,17 @@
 onFacade
-, 
+(
 orchestr
@@ -2613,52 +2613,14 @@
 tion
-Facade.getConfigService().loadDataSourceMap(
+Config
 ));%0A
@@ -2664,17 +2664,20 @@
 rvice = 
-o
+getO
 rchestra
@@ -2686,16 +2686,18 @@
 onFacade
+()
 .getConf

modify OrchestrationMasterSlaveDataSource()