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

import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingsphere.jdbc.orchestration.internal.OrchestrationProxyConfiguration;
import io.shardingsphere.jdbc.orchestration.internal.config.ConfigurationService;
import io.shardingsphere.jdbc.orchestration.internal.eventbus.jdbc.JDBCEventBusEvent;
import io.shardingsphere.jdbc.orchestration.internal.eventbus.jdbc.JDBCEventBusInstance;
import io.shardingsphere.jdbc.orchestration.internal.eventbus.proxy.ProxyEventBusEvent;
import io.shardingsphere.jdbc.orchestration.internal.eventbus.proxy.ProxyEventBusInstance;
import io.shardingsphere.jdbc.orchestration.internal.listener.ListenerManager;
import io.shardingsphere.jdbc.orchestration.internal.state.StateNode;
import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenter;
import io.shardingsphere.jdbc.orchestration.reg.listener.DataChangedEvent;
import io.shardingsphere.jdbc.orchestration.reg.listener.EventListener;

/**
 * Data source listener manager.
 *
 * @author caohao
 * @author panjuan
 */
public final class DataSourceListenerManager implements ListenerManager {
    
    private final StateNode stateNode;
    
    private final RegistryCenter regCenter;
    
    private final ConfigurationService configService;
    
    private final DataSourceService dataSourceService;
    
    public DataSourceListenerManager(final String name, final RegistryCenter regCenter) {
        stateNode = new StateNode(name);
        this.regCenter = regCenter;
        configService = new ConfigurationService(name, regCenter);
        dataSourceService = new DataSourceService(name, regCenter);
    }
    
    @Override
    public void start(final ShardingDataSource shardingDataSource) {
        regCenter.watch(stateNode.getDataSourcesNodeFullPath(), new EventListener() {
            
            @Override
            public void onChange(final DataChangedEvent event) {
                if (DataChangedEvent.Type.UPDATED == event.getEventType() || DataChangedEvent.Type.DELETED == event.getEventType()) {
                    JDBCEventBusEvent JDBCEventBusEvent = new JDBCEventBusEvent();
                    JDBCEventBusEvent.getDisabledDataSourceNames().addAll(dataSourceService.getDisabledDataSourceNames());
                    JDBCEventBusInstance.getInstance().post(JDBCEventBusEvent);
                }
            }
        });
    }
    
    @Override
    public void start(final MasterSlaveDataSource masterSlaveDataSource) {
        regCenter.watch(stateNode.getDataSourcesNodeFullPath(), new EventListener() {
            
            @Override
            public void onChange(final DataChangedEvent event) {
                if (DataChangedEvent.Type.UPDATED == event.getEventType() || DataChangedEvent.Type.DELETED == event.getEventType()) {
                    MasterSlaveRuleConfiguration masterSlaveRuleConfiguration = dataSourceService.getAvailableMasterSlaveRuleConfiguration();
                    if (masterSlaveRuleConfiguration.getSlaveDataSourceNames().isEmpty()) {
                        throw new ShardingException("No available slave datasource, can't apply the configuration!");
                    } 
                    masterSlaveDataSource.renew(dataSourceService.getAvailableDataSources(), masterSlaveRuleConfiguration);
                }
            }
        });
    }
    
    @Override
    public void start() {
        regCenter.watch(stateNode.getDataSourcesNodeFullPath(), new EventListener() {
            
            @Override
            public void onChange(final DataChangedEvent event) {
                if (DataChangedEvent.Type.UPDATED == event.getEventType() || DataChangedEvent.Type.DELETED == event.getEventType()) {
                    OrchestrationProxyConfiguration availableYamlProxyConfiguration = dataSourceService.getAvailableYamlProxyConfiguration();
                    if (availableYamlProxyConfiguration.getShardingRule().getTables().isEmpty() && availableYamlProxyConfiguration.getMasterSlaveRule().getSlaveDataSourceNames().isEmpty()) {
                        throw new ShardingException("No available slave datasource, can't apply the configuration!");
                    }
                    ProxyEventBusInstance.getInstance().post(new ProxyEventBusEvent(dataSourceService.getAvailableDataSourceParameters(), availableYamlProxyConfiguration));
                }
            }
        });
    }
}

@@ -690,79 +690,8 @@
 e;%0A%0A
-import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration;%0A
 impo
@@ -741,24 +741,24 @@
 gException;%0A
+
 import io.sh
@@ -980,90 +980,8 @@
 on;%0A
-import io.shardingsphere.jdbc.orchestration.internal.config.ConfigurationService;%0A
 impo
@@ -1121,36 +1121,157 @@
 l.eventbus.jdbc.
-JDBC
+masterslave.MasterSlaveEventBusInstance;%0Aimport io.shardingsphere.jdbc.orchestration.internal.eventbus.jdbc.sharding.Sharding
 EventBusInstance
@@ -2073,67 +2073,8 @@
    %0A
-    private final ConfigurationService configService;%0A    %0A
     
@@ -2300,75 +2300,8 @@
 er;%0A
-        configService = new ConfigurationService(name, regCenter);%0A
     
@@ -2816,20 +2816,20 @@
 usEvent 
-JDBC
+jdbc
 EventBus
@@ -2873,36 +2873,36 @@
                 
-JDBC
+jdbc
 EventBusEvent.ge
@@ -2996,36 +2996,40 @@
                 
-JDBC
+Sharding
 EventBusInstance
@@ -3048,20 +3048,20 @@
 ().post(
-JDBC
+jdbc
 EventBus
@@ -3556,484 +3556,278 @@
     
-MasterSlaveRuleConfiguration masterSlaveRuleConfiguration = dataSourceService.getAvailableMasterSlaveRuleConfiguration();%0A                    if (masterSlaveRuleConfiguration.getSlaveDataSourceNames().isEmpty()) %7B%0A                        throw new ShardingException(%22No available slave datas
+JDBCEventBusEvent jdbcEventBusEvent = new JDBCEventBusEvent();%0A                    jdbcEventBusEvent.getDisabledDataSourceNames().addAll(dataSourceService.getDisabledDataS
 ource
-, can't apply the configuration!%22);%0A                    %7D %0A                    masterSlaveDataSource.renew(dataSourceService.getAvailableDataSour
+Names());%0A                    MasterSlaveEventBusInstance.getInstan
 ce
-s
 ()
-, masterSlaveRuleConfiguration
+.post(jdbcEventBusEvent
 );%0A 

remove config service