/*
 * 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.maven.surefire.api.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Random;

import org.apache.maven.surefire.api.runorder.RunEntryStatisticsMap;
import org.apache.maven.surefire.api.testset.RunOrderParameters;

/**
 * Applies the final runorder of the tests
 *
 * @author Kristian Rosenvold
 */
public class DefaultRunOrderCalculator implements RunOrderCalculator {
    private final Comparator<Class<?>> sortOrder;

    private final RunOrder[] runOrder;

    private final RunOrderParameters runOrderParameters;

    private final int threadCount;

    private final Random random;

    public DefaultRunOrderCalculator(RunOrderParameters runOrderParameters, int threadCount) {
        this.runOrderParameters = runOrderParameters;
        this.threadCount = threadCount;
        this.runOrder = runOrderParameters.getRunOrder();
        this.sortOrder = this.runOrder.length > 0 ? getSortOrderComparator(this.runOrder[0]) : null;
        Long runOrderRandomSeed = runOrderParameters.getRunOrderRandomSeed();
        random = new Random(runOrderRandomSeed == null ? System.nanoTime() : runOrderRandomSeed);
    }

    @Override
    @SuppressWarnings("checkstyle:magicnumber")
    public TestsToRun orderTestClasses(TestsToRun scannedClasses) {
        List<Class<?>> result = new ArrayList<>(512);

        for (Class<?> scannedClass : scannedClasses) {
            result.add(scannedClass);
        }

        orderTestClasses(result, runOrder.length != 0 ? runOrder[0] : null);
        return new TestsToRun(new LinkedHashSet<>(result));
    }

    private void orderTestClasses(List<Class<?>> testClasses, RunOrder runOrder) {
        if (RunOrder.RANDOM.equals(runOrder)) {
            Collections.shuffle(testClasses, random);
        } else if (RunOrder.FAILEDFIRST.equals(runOrder)) {
            RunEntryStatisticsMap stat = RunEntryStatisticsMap.fromFile(runOrderParameters.getRunStatisticsFile());
            List<Class<?>> prioritized = stat.getPrioritizedTestsByFailureFirst(testClasses);
            testClasses.clear();
            testClasses.addAll(prioritized);

        } else if (RunOrder.BALANCED.equals(runOrder)) {
            RunEntryStatisticsMap stat = RunEntryStatisticsMap.fromFile(runOrderParameters.getRunStatisticsFile());
            List<Class<?>> prioritized = stat.getPrioritizedTestsClassRunTime(testClasses, threadCount);
            testClasses.clear();
            testClasses.addAll(prioritized);

        } else if (sortOrder != null) {
            testClasses.sort(sortOrder);
        }
    }

    private static Comparator<Class<?>> getSortOrderComparator(RunOrder runOrder) {
        if (RunOrder.ALPHABETICAL.equals(runOrder)) {
            return getAlphabeticalComparator();
        } else if (RunOrder.REVERSE_ALPHABETICAL.equals(runOrder)) {
            return getReverseAlphabeticalComparator();
        } else if (RunOrder.HOURLY.equals(runOrder)) {
            final int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
            return ((hour % 2) == 0) ? getAlphabeticalComparator() : getReverseAlphabeticalComparator();
        } else {
            return null;
        }
    }

    private static Comparator<Class<?>> getReverseAlphabeticalComparator() {
        return (o1, o2) -> o2.getName().compareTo(o1.getName());
    }

    private static Comparator<Class<?>> getAlphabeticalComparator() {
        return Comparator.comparing(Class::getName);
    }
}
