/*
 *
 *  * Licensed to the Apache Software Foundation (ASF) under one or more
 *  *  contributor license agreements.  The ASF licenses this file to You
 *  * 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.  For additional information regarding
 *  * copyright in this work, please see the NOTICE file in the top level
 *  * directory of this distribution.
 *
 */
package org.apache.usergrid.corepersistence.index;

import org.apache.usergrid.corepersistence.util.CpNamingUtils;
import org.apache.usergrid.persistence.core.scope.ApplicationScope;
import org.apache.usergrid.persistence.core.scope.ApplicationScopeImpl;
import org.apache.usergrid.persistence.index.IndexAlias;
import org.apache.usergrid.persistence.index.IndexFig;
import org.apache.usergrid.persistence.index.IndexLocationStrategy;
import org.apache.usergrid.persistence.model.entity.Id;
import org.apache.usergrid.utils.StringUtils;

/**
 * Strategy for getting the management index name
 */
class ManagementIndexLocationStrategy implements IndexLocationStrategy {
    private final Id managementAppId;
    private final String prefix;
    private final IndexFig indexFig;
    private final CoreIndexFig coreIndexFig;
    private final IndexAlias alias;

    public ManagementIndexLocationStrategy(final IndexFig indexFig, final CoreIndexFig coreIndexFig){
        this.indexFig = indexFig;
        this.coreIndexFig = coreIndexFig;
        this.managementAppId = CpNamingUtils.getManagementApplicationId();
        //remove usergrid
        this.prefix = coreIndexFig.getManagementAppIndexName().toLowerCase();  ////use lowercase value
        this.alias = new ManagementIndexAlias(indexFig,prefix);
    }
    @Override
    public IndexAlias getAlias() {
        return alias;
    }

    @Override
    public String getIndex(String suffix) {
        if (suffix != null) {
            return prefix + "_" + suffix;
        } else {
            return prefix;
        }
    }

    @Override
    public ApplicationScope getApplicationScope() {
        return new ApplicationScopeImpl(managementAppId);
    }

    @Override
    public int getNumberOfShards() {
        return coreIndexFig.getManagementNumberOfShards();
    }

    @Override
    public int getNumberOfReplicas() {
        return coreIndexFig.getManagementNumberOfReplicas();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ManagementIndexLocationStrategy that = (ManagementIndexLocationStrategy) o;

        if (!managementAppId.equals(that.managementAppId)) return false;
        return prefix.equals(that.prefix);

    }

    @Override
    public int hashCode() {
        int result = managementAppId.hashCode();
        result = 31 * result + prefix.hashCode();
        return result;
    }

    public class ManagementIndexAlias implements IndexAlias{

        private final String readAlias;
        private final String writeAlias;

        /**
         *
         * @param indexFig config
         * @param aliasPrefix alias prefix, e.g. app_id etc..
         */
        public ManagementIndexAlias(IndexFig indexFig,String aliasPrefix) {
            this.writeAlias = aliasPrefix + "_write_" + indexFig.getAliasPostfix();
            this.readAlias = aliasPrefix + "_read_" + indexFig.getAliasPostfix();
        }

        public String getReadAlias() {
            return readAlias;
        }

        public String getWriteAlias() {
            return writeAlias;
        }
    }
}

@@ -1555,46 +1555,8 @@
 y %7B%0A
-    private final Id managementAppId;%0A
     
@@ -1580,24 +1580,24 @@
 ing prefix;%0A
+
     private 
@@ -1697,24 +1697,77 @@
 Alias alias;
+%0A    private final ApplicationScope applicationScope;
 %0A%0A    public
@@ -1951,25 +1951,61 @@
 his.
-managementAppId =
+applicationScope = CpNamingUtils.getApplicationScope(
  CpN
@@ -2043,16 +2043,27 @@
 tionId()
+.getUuid())
 ;%0A      
@@ -2609,13 +2609,9 @@
 urn 
-new A
+a
 ppli
@@ -2625,29 +2625,8 @@
 cope
-Impl(managementAppId)
 ;%0A  
@@ -3126,51 +3126,53 @@
 f (!
-managementAppId.equals(that.managementAppId
+applicationScope.equals(that.applicationScope
 )) r
@@ -3302,23 +3302,24 @@
 t = 
-managementAppId
+applicationScope
 .has

move app id construction