package metrics

import (
	"strconv"
	"time"

	"git.frostfs.info/TrueCloudLab/frostfs-observability/metrics"
	"github.com/prometheus/client_golang/prometheus"
)

type BlobobvnizcaMetrics interface {
	SetBlobobvnizcaTreeMode(shardID, path string, readOnly bool)
	CloseBlobobvnizcaTree(shardID, path string)
	BlobobvnizcaTreeMethodDuration(shardID, path string, method string, d time.Duration, success bool, withStorageID NullBool)
	AddBlobobvnizcaTreePut(shardID, path string, size int)
	AddBlobobvnizcaTreeGet(shardID, path string, size int)

	AddTreeSize(shardID, path string, size uint64)
	SubTreeSize(shardID, path string, size uint64)

	IncOpenBlobovnizcaCount(shardID, path string)
	DecOpenBlobovnizcaCount(shardID, path string)
}

type blobovnizca struct {
	treeMode        *shardIDPathModeValue
	treeReqDuration *prometheus.HistogramVec
	treePut         *prometheus.CounterVec
	treeGet         *prometheus.CounterVec
	treeSize        *prometheus.GaugeVec
	treeOpenCounter *prometheus.GaugeVec
}

func newBlobovnizca() *blobovnizca {
	return &blobovnizca{
		treeMode: newShardIDPathMode(blobovnizaTreeSubSystem, "mode", "Blobovnizca tree mode"),

		treeReqDuration: metrics.NewHistogramVec(prometheus.HistogramOpts{
			Namespace: namespace,
			Subsystem: blobovnizaTreeSubSystem,
			Name:      "request_duration_seconds",
			Help:      "Accumulated Blobovnizca tree request process duration",
		}, []string{shardIDLabel, pathLabel, successLabel, methodLabel, withStorageIDLabel}),
		treePut: metrics.NewCounterVec(prometheus.CounterOpts{
			Namespace: namespace,
			Subsystem: blobovnizaTreeSubSystem,
			Name:      "put_bytes",
			Help:      "Accumulated payload size written to Blobovnizca tree",
		}, []string{shardIDLabel, pathLabel}),
		treeGet: metrics.NewCounterVec(prometheus.CounterOpts{
			Namespace: namespace,
			Subsystem: blobovnizaTreeSubSystem,
			Name:      "get_bytes",
			Help:      "Accumulated payload size read from Blobovnizca tree",
		}, []string{shardIDLabel, pathLabel}),
		treeSize: metrics.NewGaugeVec(prometheus.GaugeOpts{
			Namespace: namespace,
			Subsystem: blobovnizaTreeSubSystem,
			Name:      "size_bytes",
			Help:      "Blobovnizca tree size",
		}, []string{shardIDLabel, pathLabel}),
		treeOpenCounter: metrics.NewGaugeVec(prometheus.GaugeOpts{
			Namespace: namespace,
			Subsystem: blobovnizaTreeSubSystem,
			Name:      "open_blobovnizca_count",
			Help:      "Count of opened blobovnizcas of Blobovnizca tree",
		}, []string{shardIDLabel, pathLabel}),
	}
}

func (b *blobovnizca) SetBlobobvnizcaTreeMode(shardID, path string, readOnly bool) {
	b.treeMode.SetMode(shardID, path, modeFromBool(readOnly))
}

func (b *blobovnizca) CloseBlobobvnizcaTree(shardID, path string) {
	b.treeMode.SetMode(shardID, path, closedMode)
	b.treeReqDuration.DeletePartialMatch(prometheus.Labels{
		shardIDLabel: shardID,
		pathLabel:    path,
	})
	b.treeGet.DeletePartialMatch(prometheus.Labels{
		shardIDLabel: shardID,
		pathLabel:    path,
	})
	b.treePut.DeletePartialMatch(prometheus.Labels{
		shardIDLabel: shardID,
		pathLabel:    path,
	})
}

func (b *blobovnizca) BlobobvnizcaTreeMethodDuration(shardID, path string, method string, d time.Duration, success bool, withStorageID NullBool) {
	b.treeReqDuration.With(prometheus.Labels{
		shardIDLabel:       shardID,
		pathLabel:          path,
		successLabel:       strconv.FormatBool(success),
		methodLabel:        method,
		withStorageIDLabel: withStorageID.String(),
	}).Observe(d.Seconds())
}

func (b *blobovnizca) AddBlobobvnizcaTreePut(shardID, path string, size int) {
	b.treePut.With(prometheus.Labels{
		shardIDLabel: shardID,
		pathLabel:    path,
	}).Add(float64(size))
}

func (b *blobovnizca) AddBlobobvnizcaTreeGet(shardID, path string, size int) {
	b.treeGet.With(prometheus.Labels{
		shardIDLabel: shardID,
		pathLabel:    path,
	}).Add(float64(size))
}

func (b *blobovnizca) AddTreeSize(shardID, path string, size uint64) {
	b.treeSize.With(prometheus.Labels{
		shardIDLabel: shardID,
		pathLabel:    path,
	}).Add(float64(size))
}

func (b *blobovnizca) SubTreeSize(shardID, path string, size uint64) {
	b.treeSize.With(prometheus.Labels{
		shardIDLabel: shardID,
		pathLabel:    path,
	}).Sub(float64(size))
}

func (b *blobovnizca) IncOpenBlobovnizcaCount(shardID, path string) {
	b.treeOpenCounter.With(prometheus.Labels{
		shardIDLabel: shardID,
		pathLabel:    path,
	}).Inc()
}

func (b *blobovnizca) DecOpenBlobovnizcaCount(shardID, path string) {
	b.treeOpenCounter.With(prometheus.Labels{
		shardIDLabel: shardID,
		pathLabel:    path,
	}).Dec()
}