/*
 * 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.JdbcStateEventBusEvent;
import io.shardingsphere.jdbc.orchestration.internal.eventbus.jdbc.state.JdbcStateEventBusInstance;
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.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() && StateNodeStatus.DISABLED.toString().equalsIgnoreCase(regCenter.get(event.getKey()))) {
                    JdbcStateEventBusEvent jdbcStateEventBusEvent = new JdbcStateEventBusEvent();
                    jdbcStateEventBusEvent.getCircuitBreakerDataSource().addAll(configService.loadDataSourceMap().keySet());
                    JdbcStateEventBusInstance.getInstance().post(jdbcStateEventBusEvent);
                }
            }
        });
    }
    
    @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() && StateNodeStatus.DISABLED.toString().equalsIgnoreCase(regCenter.get(event.getKey()))) {
                    JdbcStateEventBusEvent jdbcStateEventBusEvent = new JdbcStateEventBusEvent();
                    jdbcStateEventBusEvent.getCircuitBreakerDataSource().addAll(configService.loadDataSourceMap().keySet());
                    JdbcStateEventBusInstance.getInstance().post(jdbcStateEventBusEvent);
                }
            }
        });
    }
    
    @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()));
                }
            }
        });
    }
}

@@ -891,33 +891,43 @@
 .jdbc.state.
-JdbcState
+circuit.JdbcCircuit
 EventBusEven
@@ -1002,25 +1002,35 @@
 c.state.
-JdbcState
+circuit.JdbcCircuit
 EventBus
@@ -1659,16 +1659,45 @@
 tener;%0A%0A
+import java.util.LinkedList;%0A
 import j
@@ -2609,36 +2609,60 @@
 t.getEventType()
- && 
+) %7B%0A                    if (
 StateNodeStatus.
@@ -2756,165 +2756,87 @@
     
-JdbcStateEventBusEvent jdbcStateEventBusEvent = new JdbcStateEventBusEvent();%0A                    jdbcStateEventBusEvent.getCircuitBreakerDataSource().addAll
+    JdbcCircuitEventBusInstance.getInstance().post(new JdbcCircuitEventBusEvent
 (con
@@ -2867,32 +2867,33 @@
 eMap().keySet())
+)
 ;%0A              
@@ -2902,77 +2902,166 @@
     
-JdbcStateEventBusInstance.getInstance().post(jdbcStateEventBusEvent);
+%7D else %7B%0A                        JdbcCircuitEventBusInstance.getInstance().post(new JdbcCircuitEventBusEvent(new LinkedList%3CString%3E()));%0A                    %7D
 %0A   
@@ -3475,12 +3475,36 @@
 pe()
- && 
+) %7B%0A                    if (
 Stat
@@ -3610,165 +3610,87 @@
     
-JdbcStateEventBusEvent jdbcStateEventBusEvent = new JdbcStateEventBusEvent();%0A                    jdbcStateEventBusEvent.getCircuitBreakerDataSource().addAll
+    JdbcCircuitEventBusInstance.getInstance().post(new JdbcCircuitEventBusEvent
 (con
@@ -3729,16 +3729,17 @@
 eySet())
+)
 ;%0A      
@@ -3756,77 +3756,166 @@
     
-JdbcStateEventBusInstance.getInstance().post(jdbcStateEventBusEvent);
+%7D else %7B%0A                        JdbcCircuitEventBusInstance.getInstance().post(new JdbcCircuitEventBusEvent(new LinkedList%3CString%3E()));%0A                    %7D
 %0A   

rewrite shardingStart