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

import io.shardingsphere.core.rule.DataSourceParameter;
import io.shardingsphere.jdbc.orchestration.internal.config.ConfigurationService;
import io.shardingsphere.jdbc.orchestration.internal.eventbus.jdbc.state.circuit.JdbcCircuitEventBusEvent;
import io.shardingsphere.jdbc.orchestration.internal.eventbus.jdbc.state.circuit.JdbcCircuitEventBusInstance;
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.internal.state.StateNodeStatus;
import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenter;
import io.shardingsphere.jdbc.orchestration.reg.listener.DataChangedEvent;
import io.shardingsphere.jdbc.orchestration.reg.listener.EventListener;

import java.util.LinkedList;
import java.util.Map;

/**
 * Instance listener manager.
 *
 * @author caohao
 * @author panjuan
 */
public final class InstanceListenerManager implements ListenerManager {
    
    private final StateNode stateNode;
    
    private final RegistryCenter regCenter;
    
    private final ConfigurationService configService;
    
    public InstanceListenerManager(final String name, final RegistryCenter regCenter) {
        stateNode = new StateNode(name);
        this.regCenter = regCenter;
        configService = new ConfigurationService(name, regCenter);
    }
    
    @Override
    public void shardingStart() {
        regCenter.watch(stateNode.getInstancesNodeFullPath(OrchestrationInstance.getInstance().getInstanceId()), new EventListener() {
            
            @Override
            public void onChange(final DataChangedEvent event) {
                if (DataChangedEvent.Type.UPDATED == event.getEventType()) {
                    if (StateNodeStatus.DISABLED.toString().equalsIgnoreCase(regCenter.get(event.getKey()))) {
                        JdbcCircuitEventBusInstance.getInstance().post(new JdbcCircuitEventBusEvent(configService.loadDataSourceMap().keySet()));
                    } else {
                        JdbcCircuitEventBusInstance.getInstance().post(new JdbcCircuitEventBusEvent(new LinkedList<String>()));
                    }
                }
            }
        });
    }
    
    @Override
    public void masterSlaveStart() {
        regCenter.watch(stateNode.getInstancesNodeFullPath(OrchestrationInstance.getInstance().getInstanceId()), new EventListener() {
            
            @Override
            public void onChange(final DataChangedEvent event) {
                if (DataChangedEvent.Type.UPDATED == event.getEventType()) {
                    if (StateNodeStatus.DISABLED.toString().equalsIgnoreCase(regCenter.get(event.getKey()))) {
                        JdbcCircuitEventBusInstance.getInstance().post(new JdbcCircuitEventBusEvent(configService.loadDataSourceMap().keySet()));
                    } else {
                        JdbcCircuitEventBusInstance.getInstance().post(new JdbcCircuitEventBusEvent(new LinkedList<String>()));
                    }
                }
            }
        });
    }
    
    @Override
    public void proxyStart() {
        regCenter.watch(stateNode.getInstancesNodeFullPath(OrchestrationInstance.getInstance().getInstanceId()), new EventListener() {
            
            @Override
            public void onChange(final DataChangedEvent event) {
                if (DataChangedEvent.Type.UPDATED == event.getEventType()) {
                    Map<String, DataSourceParameter> dataSourceParameterMap = configService.loadDataSources();
                    if (StateNodeStatus.DISABLED.toString().equalsIgnoreCase(regCenter.get(event.getKey()))) {
                        dataSourceParameterMap.clear();
                    }
                    ProxyEventBusInstance.getInstance().post(new ProxyEventBusEvent(dataSourceParameterMap, configService.loadProxyConfiguration()));
                }
            }
        });
    }
}

@@ -688,64 +688,8 @@
 e;%0A%0A
-import io.shardingsphere.core.rule.DataSourceParameter;%0A
 impo
@@ -762,24 +762,24 @@
 ionService;%0A
+
 import io.sh
@@ -987,187 +987,8 @@
 ce;%0A
-import io.shardingsphere.jdbc.orchestration.internal.eventbus.proxy.ProxyEventBusEvent;%0Aimport io.shardingsphere.jdbc.orchestration.internal.eventbus.proxy.ProxyEventBusInstance;%0A
 impo
@@ -1424,16 +1424,16 @@
 tener;%0A%0A
+
 import j
@@ -1456,30 +1456,8 @@
 ist;
-%0Aimport java.util.Map;
 %0A%0A/*
@@ -4070,119 +4070,8 @@
 ) %7B%0A
-                    Map%3CString, DataSourceParameter%3E dataSourceParameterMap = configService.loadDataSources();%0A
     
@@ -4205,37 +4205,125 @@
     
-dataSourceParameterMap.clear(
+JdbcCircuitEventBusInstance.getInstance().post(new JdbcCircuitEventBusEvent(configService.loadDataSources().keySet())
 );%0A 
@@ -4334,32 +4334,39 @@
                %7D
+ else %7B
 %0A               
@@ -4370,21 +4370,31 @@
         
-Proxy
+    JdbcCircuit
 EventBus
@@ -4425,21 +4425,27 @@
 ost(new 
-Proxy
+JdbcCircuit
 EventBus
@@ -4454,73 +4454,57 @@
 ent(
-dataSourceParameterMap, configService.loadProxyConfiguration()));
+new LinkedList%3CString%3E()));%0A                    %7D
 %0A   

rewrite proxyStart