/*
 * 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.orchestration.internal.registry.config.listener;

import com.google.common.base.Strings;
import io.shardingsphere.api.config.rule.RuleConfiguration;
import io.shardingsphere.orchestration.internal.registry.config.event.DataSourceChangedEvent;
import io.shardingsphere.orchestration.internal.registry.config.event.IgnoredChangedEvent;
import io.shardingsphere.orchestration.internal.registry.config.event.MasterSlaveRuleChangedEvent;
import io.shardingsphere.orchestration.internal.registry.config.event.SchemaAddChangedEvent;
import io.shardingsphere.orchestration.internal.registry.config.event.SchemaDeleteChangedEvent;
import io.shardingsphere.orchestration.internal.registry.config.event.ShardingRuleChangedEvent;
import io.shardingsphere.orchestration.internal.registry.config.node.ConfigurationNode;
import io.shardingsphere.orchestration.internal.registry.config.service.ConfigurationService;
import io.shardingsphere.orchestration.internal.registry.listener.PostShardingOrchestrationEventListener;
import io.shardingsphere.orchestration.internal.registry.listener.ShardingOrchestrationEvent;
import io.shardingsphere.orchestration.reg.api.RegistryCenter;
import io.shardingsphere.orchestration.reg.listener.DataChangedEvent;
import io.shardingsphere.orchestration.reg.listener.DataChangedEvent.ChangedType;
import io.shardingsphere.orchestration.yaml.ConfigurationYamlConverter;

import java.util.Collection;
import java.util.LinkedList;

/**
 * Schema changed listener.
 *
 * @author panjuan
 */
public final class SchemaChangedListener extends PostShardingOrchestrationEventListener {
    
    private final ConfigurationService configurationService;
    
    private final ConfigurationNode configurationNode;
    
    private final Collection<String> existedSchemaNames = new LinkedList<>();
    
    public SchemaChangedListener(final String name, final RegistryCenter regCenter, final Collection<String> shardingSchemaNames, final ConfigurationService configurationService) {
        super(regCenter, new ConfigurationNode(name).getSchemaPath());
        this.configurationService = configurationService;
        configurationNode = new ConfigurationNode(name);
        existedSchemaNames.addAll(shardingSchemaNames);
    }
    
    @Override
    protected ShardingOrchestrationEvent createShardingOrchestrationEvent(final DataChangedEvent event) {
        String shardingSchemaName = configurationNode.getSchemaName(event.getKey());
        if (Strings.isNullOrEmpty(shardingSchemaName)) {
            return new IgnoredChangedEvent();
        }
        if (ChangedType.UPDATED == event.getChangedType()) {
            return createUpdateChangedEvent(shardingSchemaName, event);
        }
        if (ChangedType.DELETED == event.getChangedType()) {
            return createDeleteChangedEvent(shardingSchemaName);
        }
        return new IgnoredChangedEvent();
    }
    
    private ShardingOrchestrationEvent createUpdateChangedEvent(final String shardingSchemaName, final DataChangedEvent event) {
        if (existedSchemaNames.contains(shardingSchemaName)) {
            return createChangedEventForExistedSchema(event, shardingSchemaName);
        } else {
            return createChangedEventForNewSchema(shardingSchemaName);
        }
    }
    
    private ShardingOrchestrationEvent createDeleteChangedEvent(final String shardingSchemaName) {
        existedSchemaNames.remove(shardingSchemaName);
        return new SchemaDeleteChangedEvent(shardingSchemaName);
    }
    
    private ShardingOrchestrationEvent createChangedEventForNewSchema(final String shardingSchemaName) {
        if (isSufficientToInitialize(shardingSchemaName)) {
            existedSchemaNames.add(shardingSchemaName);
            return createSchemaChangedEvent(shardingSchemaName);
        }
        return new IgnoredChangedEvent();
    }
    
    private ShardingOrchestrationEvent createChangedEventForExistedSchema(final DataChangedEvent event, final String shardingSchemaName) {
        if (event.getKey().equals(configurationNode.getDataSourcePath(shardingSchemaName))) {
            return createDataSourceChangedEvent(shardingSchemaName, event);
        }
        return createRuleChangedEvent(shardingSchemaName, event);
    }
    
    private DataSourceChangedEvent createDataSourceChangedEvent(final String shardingSchemaName, final DataChangedEvent event) {
        return new DataSourceChangedEvent(shardingSchemaName, ConfigurationYamlConverter.loadDataSourceConfigurations(event.getValue()));
    }
    
    private ShardingOrchestrationEvent createRuleChangedEvent(final String shardingSchemaName, final DataChangedEvent event) {
        return isShardingRule(event) ? getShardingRuleChangedEvent(shardingSchemaName, event.getValue()) : getMasterSlaveRuleChangedEvent(shardingSchemaName, event.getValue());
    }
    
    private boolean isShardingRule(final DataChangedEvent event) {
        return event.getValue().contains("tables:\n");
    }
    
    private ShardingRuleChangedEvent getShardingRuleChangedEvent(final String shardingSchemaName, final String ruleValue) {
        return new ShardingRuleChangedEvent(shardingSchemaName, ConfigurationYamlConverter.loadShardingRuleConfiguration(ruleValue));
    }
    
    private MasterSlaveRuleChangedEvent getMasterSlaveRuleChangedEvent(final String shardingSchemaName, final String ruleValue) {
        return new MasterSlaveRuleChangedEvent(shardingSchemaName, ConfigurationYamlConverter.loadMasterSlaveRuleConfiguration(ruleValue));
    }
    
    private boolean isSufficientToInitialize(final String shardingSchemaName) {
        return configurationService.hasDataSourceConfiguration(shardingSchemaName) && configurationService.hasRuleConfiguration(shardingSchemaName);
    }
    
    private SchemaAddChangedEvent createSchemaChangedEvent(final String shardingSchemaName) {
        return new SchemaAddChangedEvent(shardingSchemaName, configurationService.loadDataSourceConfigurations(shardingSchemaName), createRuleConfiguration(shardingSchemaName));
    }
    
    private RuleConfiguration createRuleConfiguration(final String shardingSchemaName) {
        return configurationService.isShardingRule(shardingSchemaName) ? configurationService.loadShardingRuleConfiguration(shardingSchemaName) : configurationService.loadMasterSlaveRuleConfiguration(shardingSchemaName);
    }
}

@@ -3819,20 +3819,9 @@
    %7D
- else %7B%0A    
+%0A
     
@@ -3879,34 +3879,24 @@
 chemaName);%0A
-        %7D%0A
     %7D%0A    %0A 
@@ -3935,30 +3935,63 @@
 Event create
-Delete
+ChangedEventForExistedSchema(final Data
 ChangedEvent
@@ -3982,33 +3982,40 @@
 DataChangedEvent
-(
+ event, 
 final String sha
@@ -4045,54 +4045,179 @@
     
-existedSchemaNames.remove(shardingSchemaName);
+if (event.getKey().equals(configurationNode.getDataSourcePath(shardingSchemaName))) %7B%0A            return createDataSourceChangedEvent(shardingSchemaName, event);%0A        %7D
 %0A   
@@ -4232,23 +4232,17 @@
 urn 
-new SchemaDelet
+createRul
 eCha
@@ -4261,32 +4261,39 @@
 ardingSchemaName
+, event
 );%0A    %7D%0A    %0A  
@@ -4688,47 +4688,14 @@
 eate
-ChangedEventForExistedSchema(final Data
+Delete
 Chan
@@ -4698,32 +4698,25 @@
 ChangedEvent
- event, 
+(
 final String
@@ -4750,69 +4750,33 @@
     
-if (event.getKey().equals(configurationNode.getDataSourcePath
+existedSchemaNames.remove
 (sha
@@ -4791,32 +4791,26 @@
 emaName)
-)) %7B%0A   
+;%0A
         
  return 
@@ -4801,39 +4801,38 @@
         
- 
 return 
-createDataSourc
+new SchemaDelet
 eChanged
@@ -4851,38 +4851,27 @@
 ngSchemaName
-, event);%0A    
+);%0A
     %7D%0A      
@@ -4872,75 +4872,18 @@
     
+%0A
     
-return createRuleChangedEvent(shardingSchemaName, event);
 %0A    
-%7D
 %0A   

change position for functions