/*
 * 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.skywalking.oap.server.core.query.type;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.skywalking.oap.server.core.analysis.metrics.DataTable;

/**
 * HeatMap represents the value distribution in the defined buckets.
 *
 * @since 8.0.0
 */
@Getter
public class HeatMap {
    private List<HeatMapColumn> values = new ArrayList<>(10);
    private List<Bucket> buckets = null;

    public void addBucket(Bucket bucket) {
        this.buckets.add(bucket);
    }

    /**
     * Build one heatmap value column based on rawdata in the storage and row id.
     *
     * @param id      of the row
     * @param rawdata literal string, represent a {@link DataTable}
     */
    public void buildColumn(String id, String rawdata, int defaultValue) {
        DataTable dataset = new DataTable(rawdata);

        final List<String> sortedKeys = dataset.sortedKeys(new KeyComparator(true));
        if (buckets == null) {
            buckets = new ArrayList<>(dataset.size());
            for (int i = 0; i < sortedKeys.size(); i++) {
                final Bucket bucket = new Bucket();
                final String minValue = sortedKeys.get(i);

                if (Bucket.INFINITE_NEGATIVE.equals(minValue)) {
                    bucket.infiniteMin();
                } else {
                    bucket.setMin(Integer.parseInt(minValue));
                }

                if (i == sortedKeys.size() - 1) {
                    // last element
                    bucket.infiniteMax();
                } else {
                    final String max = sortedKeys.get(i + 1);
                    if (Bucket.INFINITE_POSITIVE.equals(max)) {
                        // If reach the infinite positive before the last element, ignore all other.
                        // Only for fail safe.
                        bucket.infiniteMax();
                        break;
                    } else {
                        bucket.setMax(Integer.parseInt(max));
                    }
                }
                this.addBucket(bucket);
            }
        }

        HeatMap.HeatMapColumn column = new HeatMap.HeatMapColumn();
        column.setId(id);
        sortedKeys.forEach(key -> {
            if (dataset.hasKey(key)) {
                column.addValue(dataset.get(key));
            } else {
                column.addValue((long) defaultValue);
            }
        });
        values.add(column);
    }

    public void fixMissingColumns(List<String> ids, int defaultValue) {
        for (int i = 0; i < ids.size(); i++) {
            final String expectedId = ids.get(i);
            boolean found = false;
            for (final HeatMapColumn value : values) {
                if (expectedId.equals(value.id)) {
                    found = true;
                }
            }
            if (!found) {
                final HeatMapColumn emptyColumn = buildMissingColumn(expectedId, defaultValue);
                values.add(i, emptyColumn);
            }
        }
    }

    private HeatMapColumn buildMissingColumn(String id, int defaultValue) {
        HeatMapColumn column = new HeatMapColumn();
        column.setId(id);
        buckets.forEach(bucket -> {
            column.addValue((long) defaultValue);
        });
        return column;
    }

    @Getter
    public static class HeatMapColumn {
        @Setter
        private String id;
        private List<Long> values = new ArrayList<>();

        public void addValue(Long value) {
            values.add(value);
        }
    }

    @RequiredArgsConstructor
    public static class KeyComparator implements Comparator<String> {
        private final boolean asc;

        @Override
        public int compare(final String key1, final String key2) {
            int result;

            if (key1.equals(key2)) {
                result = 0;
            } else if (Bucket.INFINITE_NEGATIVE.equals(key1) || Bucket.INFINITE_POSITIVE.equals(key2)) {
                result = -1;
            } else if (Bucket.INFINITE_NEGATIVE.equals(key2) || Bucket.INFINITE_POSITIVE.equals(key1)) {
                result = 1;
            } else {
                result = Integer.parseInt(key1) - Integer.parseInt(key2);
            }

            return asc ? result : 0 - result;
        }
    }
}

@@ -1320,19 +1320,32 @@
 kets = n
-ull
+ew ArrayList%3C%3E()
 ;%0A%0A    p
@@ -1865,74 +1865,21 @@
 kets
- == null) %7B%0A            buckets = new ArrayList%3C%3E(dataset.size());
+.isEmpty()) %7B
 %0A   

fix NPE when fetch empty readHeatMap metrics. (#4891)