// Copyright 2021 PingCAP, Inc.
//
// Licensed 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package tikvapi

import (
	"crypto/tls"
	"fmt"
	"net"
	"net/http"
	"strconv"
	"time"

	"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
	"github.com/pingcap/tidb-operator/pkg/util"
	"github.com/prometheus/prom2json"
)

const (
	DefaultTimeout        = 5 * time.Second
	metricNameRegionCount = "tikv_raftstore_region_count"
	labelNameLeaderCount  = "leader"

	metricNameCpuCoresQuota   = "tikv_server_cpu_cores_quota"
	metricNameCpuSecondsTotal = "process_cpu_seconds_total"
	metricNameStoreSizeBytes  = "tikv_store_size_bytes"
)

// TiKVClient provides tikv server's api
type TiKVClient interface {
	GetLeaderCount() (int, error)
	GetResourceUsage() (*v1alpha1.ResourceUsage, error)
}

// tikvClient is default implementation of TiKVClient
type tikvClient struct {
	url        string
	httpClient *http.Client
}

// GetLeaderCount gets region leader count from the URL
func (c *tikvClient) GetLeaderCount() (int, error) {
	fms, apiURL := util.FetchMetricFamilies(c.url, c.httpClient)
	for _, fm := range fms {
		if fm.Name == metricNameRegionCount {
			for _, m := range fm.Metrics {
				if m, ok := m.(prom2json.Metric); ok && m.Labels["type"] == labelNameLeaderCount {
					return strconv.Atoi(m.Value)
				}
			}
		}
	}
	return 0, fmt.Errorf("metric %s{type=\"%s\"} not found for %s", metricNameRegionCount, labelNameLeaderCount, apiURL)
}

// GetResourceUsage gets the CPU and storage resource usage.
func (c *tikvClient) GetResourceUsage() (*v1alpha1.ResourceUsage, error) {
	fms, apiURL := util.FetchMetricFamilies(c.url, c.httpClient)
	return util.GetResourceUsage(fms, apiURL, metricNameCpuCoresQuota, metricNameCpuSecondsTotal, metricNameStoreSizeBytes)
}

// NewTiKVClient returns a new TiKVClient
func NewTiKVClient(url string, timeout time.Duration, tlsConfig *tls.Config, disableKeepalive bool) TiKVClient {
	return &tikvClient{
		url: url,
		httpClient: &http.Client{
			Timeout: timeout,
			Transport: &http.Transport{
				TLSClientConfig:       tlsConfig,
				DisableKeepAlives:     disableKeepalive,
				ResponseHeaderTimeout: 10 * time.Second,
				TLSHandshakeTimeout:   10 * time.Second,
				DialContext: (&net.Dialer{
					Timeout: 10 * time.Second,
				}).DialContext,
			},
		},
	}
}
