/*
 * 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.internal;

import com.google.common.eventbus.Subscribe;
import io.shardingsphere.core.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.event.ShardingEventBusInstance;
import io.shardingsphere.core.jdbc.adapter.AbstractDataSourceAdapter;
import io.shardingsphere.core.jdbc.core.connection.ShardingConnection;
import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingsphere.core.rule.ShardingRule;
import io.shardingsphere.jdbc.orchestration.internal.event.config.ShardingConfigurationEventBusEvent;
import io.shardingsphere.jdbc.orchestration.internal.event.state.CircuitStateEventBusEvent;
import io.shardingsphere.jdbc.orchestration.internal.event.state.DisabledStateEventBusEvent;
import io.shardingsphere.jdbc.orchestration.internal.jdbc.datasource.CircuitBreakerDataSource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Orchestration sharding datasource.
 *
 * @author caohao
 */
@Slf4j
public final class OrchestrationShardingDataSource extends AbstractDataSourceAdapter implements AutoCloseable {
    
    @Getter
    private ShardingDataSource dataSource;
    
    private final OrchestrationFacade orchestrationFacade;
    
    private Map<String, DataSource> dataSourceMap;
    
    public OrchestrationShardingDataSource(final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig,
                                           final Map<String, Object> configMap, final Properties props, final OrchestrationFacade orchestrationFacade) throws SQLException {
        super(dataSourceMap.values());
        this.dataSource = new ShardingDataSource(dataSourceMap, new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()), configMap, props);
        this.orchestrationFacade = orchestrationFacade;
        this.orchestrationFacade.init(dataSourceMap, shardingRuleConfig, configMap, props);
        this.dataSourceMap = dataSourceMap;
        ShardingEventBusInstance.getInstance().register(this);
    }
    
    @Override
    public ShardingConnection getConnection() {
        return dataSource.getConnection();
    }
    
    @Override
    public void close() {
        dataSource.close();
        orchestrationFacade.close();
    }
    
    /**
     * Renew sharding data source.
     *
     * @param shardingEvent sharding configuration event bus event.
     * @throws SQLException sql exception
     */
    @Subscribe
    public void renew(final ShardingConfigurationEventBusEvent shardingEvent) throws SQLException {
        super.renew(shardingEvent.getDataSourceMap().values());
        dataSource = new ShardingDataSource(shardingEvent.getDataSourceMap(), shardingEvent.getShardingRule(), new LinkedHashMap<String, Object>(), shardingEvent.getProps());
    }
    
    /**
     * Renew disable dataSource names.
     *
     * @param disabledStateEventBusEvent jdbc disabled event bus event
     */
    @Subscribe
    public void renewDisabledDataSourceNames(final DisabledStateEventBusEvent disabledStateEventBusEvent) {
        Map<String, DataSource> newDataSourceMap = getAvailableDataSourceMap(disabledStateEventBusEvent.getDisabledDataSourceNames());
        dataSource = new ShardingDataSource(newDataSourceMap, dataSource.getShardingContext(), dataSource.getShardingProperties(), dataSource.getDatabaseType());
    }
    
    private Map<String, DataSource> getAvailableDataSourceMap(final Collection<String> disabledDataSourceNames) {
        Map<String, DataSource> result = new LinkedHashMap<>(dataSourceMap);
        for (String each : disabledDataSourceNames) {
            result.remove(each);
        }
        return result;
    }
    
    /**
     * Renew circuit breaker dataSource names.
     *
     * @param circuitStateEventBusEvent jdbc disabled event bus event
     */
    @Subscribe
    public void renewCircuitBreakerDataSourceNames(final CircuitStateEventBusEvent circuitStateEventBusEvent) {
        Map<String, DataSource> newDataSourceMap = getCircuitBreakerDataSourceMap();
        dataSource = new ShardingDataSource(newDataSourceMap, dataSource.getShardingContext(), dataSource.getShardingProperties(), dataSource.getDatabaseType());
    }
    
    private Map<String, DataSource> getCircuitBreakerDataSourceMap() {
        Map<String, DataSource> result = new LinkedHashMap<>();
        for (String each : dataSourceMap.keySet()) {
            result.put(each, new CircuitBreakerDataSource());
        }
        return result;
    }
}

@@ -752,40 +752,24 @@
 api.
-c
+C
 onfig
-.ShardingRuleConfiguration
+MapContext
 ;%0Aim
@@ -1044,57 +1044,8 @@
 ce;%0A
-import io.shardingsphere.core.rule.ShardingRule;%0A
 impo
@@ -1624,37 +1624,8 @@
 Map;
-%0Aimport java.util.Properties;
 %0A%0A/*
@@ -2042,28 +2042,24 @@
 e(final 
-Map%3CStr
+Shard
 ing
-, 
 DataSour
@@ -2064,179 +2064,27 @@
 urce
-%3E dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig,%0A                                           final Map%3CString, Object%3E configMap, final Properties props
+ shardingDataSource
 , fi
@@ -2164,17 +2164,39 @@
   super(
-d
+shardingDataSource.getD
 ataSourc
@@ -2199,16 +2199,18 @@
 ourceMap
+()
 .values(
@@ -2231,37 +2231,33 @@
 is.dataSource = 
-new S
+s
 hardingDataSourc
@@ -2261,103 +2261,8 @@
 urce
-(dataSourceMap, new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()), configMap, props)
 ;%0A  
@@ -2372,24 +2372,85 @@
 ap, sharding
+DataSource.getShardingContext().getShardingRule().getSharding
 RuleConfig, 
@@ -2451,19 +2451,21 @@
 nfig
+()
 , 
-c
+C
 onfigMap
 , pr
@@ -2464,15 +2464,104 @@
 gMap
-, p
+Context.getInstance().getShardingConfig(), shardingDataSource.getShardingProperties().getP
 rops
+()
 );%0A 
@@ -2588,17 +2588,39 @@
 ceMap = 
-d
+shardingDataSource.getD
 ataSourc
@@ -2619,24 +2619,26 @@
 ataSourceMap
+()
 ;%0A        Sh

rewrite OrchestrationShardingDataSource