/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. 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.
 */
package org.apache.geode.internal.cache.partitioned.rebalance;

import java.util.Set;

import org.apache.geode.distributed.DistributedMember;
import org.apache.geode.distributed.DistributedSystem;
import org.apache.geode.distributed.internal.InternalDistributedSystem;
import org.apache.geode.distributed.internal.membership.InternalDistributedMember;
import org.apache.geode.internal.cache.partitioned.rebalance.PartitionedRegionLoadModel.Bucket;
import org.apache.geode.internal.cache.partitioned.rebalance.PartitionedRegionLoadModel.Member;
import org.apache.geode.internal.cache.partitioned.rebalance.PartitionedRegionLoadModel.Move;
import org.apache.geode.internal.cache.partitioned.rebalance.PartitionedRegionLoadModel.RefusalReason;
import org.apache.geode.internal.i18n.LocalizedStrings;

public class ExplicitMoveDirector extends RebalanceDirectorAdapter {

  private PartitionedRegionLoadModel model;
  private final int bucketId;
  private final InternalDistributedMember source;
  private final InternalDistributedMember target;
  private final Object key;
  private InternalDistributedSystem ds;


  public ExplicitMoveDirector(Object key, int bucketId, DistributedMember source,
      DistributedMember target, DistributedSystem distributedSystem) {
    this.key = key;
    this.bucketId = bucketId;
    this.source = (InternalDistributedMember) source;
    this.target = (InternalDistributedMember) target;
    this.ds = (InternalDistributedSystem) distributedSystem;
  }

  @Override
  public void initialize(PartitionedRegionLoadModel model) {
    this.model = model;
  }

  @Override
  public void membershipChanged(PartitionedRegionLoadModel model) {
    initialize(model);
  }

  @Override
  public boolean nextStep() {
    Bucket bucket = model.getBuckets()[bucketId];
    Member sourceMember = model.getMember(source);
    Member targetMember = model.getMember(target);
    if (sourceMember == null) {
      throw new IllegalStateException(
          LocalizedStrings.PERCENTAGE_MOVE_DIRECTORY_SOURCE_NOT_DATA_STORE
              .toLocalizedString(model.getName(), source));
    }
    if (targetMember == null) {
      throw new IllegalStateException(
          LocalizedStrings.PERCENTAGE_MOVE_DIRECTORY_TARGET_NOT_DATA_STORE
              .toLocalizedString(model.getName(), target));
    }

    if (bucket == null) {
      throw new IllegalStateException("The bucket for key " + key + ", bucket " + bucketId
          + ", region " + model.getName() + " does not exist");
    }

    if (!bucket.getMembersHosting().contains(sourceMember)) {
      throw new IllegalStateException(
          "The bucket for key " + key + ", bucket " + bucketId + ", region " + model.getName()
              + " is not hosted by " + source + ". Members hosting: " + bucket.getMembersHosting());
    }

    RefusalReason reason =
        targetMember.willAcceptBucket(bucket, sourceMember, model.enforceUniqueZones());
    if (reason.willAccept()) {
      if (!model.moveBucket(new Move(sourceMember, targetMember, bucket))) {
        // Double check to see if the source or destination have left the DS
        Set allMembers = ds.getDistributionManager().getDistributionManagerIdsIncludingAdmin();
        if (!allMembers.contains(sourceMember)) {
          throw new IllegalStateException(
              LocalizedStrings.PERCENTAGE_MOVE_DIRECTORY_SOURCE_NOT_DATA_STORE
                  .toLocalizedString(model.getName(), source));
        }
        if (!allMembers.contains(targetMember)) {
          throw new IllegalStateException(
              LocalizedStrings.PERCENTAGE_MOVE_DIRECTORY_TARGET_NOT_DATA_STORE
                  .toLocalizedString(model.getName(), target));
        }
        throw new IllegalStateException(
            "Unable to move bucket " + bucket + " from " + sourceMember + " to " + targetMember);
      }
    } else {
      throw new IllegalStateException("Unable to move bucket for " + model.getName() + ". "
          + reason.formatMessage(sourceMember, targetMember, bucket));
    }

    return false;
  }
}
