package processors

import (
	"sort"

	"github.com/golangci/golangci-lint/pkg/logutils"
	"github.com/golangci/golangci-lint/pkg/result"
)

type textToCountMap map[string]int

type MaxSameIssues struct {
	tc    textToCountMap
	limit int
	log   logutils.Log
}

var _ Processor = &MaxSameIssues{}

func NewMaxSameIssues(limit int, log logutils.Log) *MaxSameIssues {
	return &MaxSameIssues{
		tc:    textToCountMap{},
		limit: limit,
		log:   log,
	}
}

func (MaxSameIssues) Name() string {
	return "max_same_issues"
}

func (p *MaxSameIssues) Process(issues []result.Issue) ([]result.Issue, error) {
	if p.limit <= 0 { // no limit
		return issues, nil
	}

	return filterIssues(issues, func(i *result.Issue) bool {
		p.tc[i.Text]++ // always inc for stat
		return p.tc[i.Text] <= p.limit
	}), nil
}

func (p MaxSameIssues) Finish() {
	walkStringToIntMapSortedByValue(p.tc, func(text string, count int) {
		if count > p.limit {
			p.log.Infof("%d/%d issues with text %q were hidden, use --max-same-issues",
				count-p.limit, count, text)
		}
	})
}

type kv struct {
	Key   string
	Value int
}

func walkStringToIntMapSortedByValue(m map[string]int, walk func(k string, v int)) {
	var ss []kv
	for k, v := range m {
		ss = append(ss, kv{
			Key:   k,
			Value: v,
		})
	}

	sort.Slice(ss, func(i, j int) bool {
		return ss[i].Value > ss[j].Value
	})

	for _, kv := range ss {
		walk(kv.Key, kv.Value)
	}
}
