// -*- coding: utf-8 -*-

// Copyright (C) 2019 Nippon Telegraph and Telephone Corporation.
//
// 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,
// 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 main

import (
	"fmt"
	"time"

	"github.com/spf13/cobra"
)

//
// StopWatch is stop waitch.
//
type StopWatch struct {
	start  time.Time
	points []time.Time
}

//
// NewStopWatch returns new StopWatch
//
func NewStopWatch() *StopWatch {
	return &StopWatch{
		points: []time.Time{},
	}
}

//
// Start starts time.
//
func (w *StopWatch) Start() {
	w.start = time.Now()
}

//
// Point saves time.
//
func (w *StopWatch) Point() {
	w.points = append(w.points, time.Now())
}

//
// Results returns times.
//
func (w *StopWatch) Results() ([]time.Duration, []time.Duration) {
	diffs := []time.Duration{}
	totals := []time.Duration{}

	prev := w.start
	for _, point := range w.points {
		diffs = append(diffs, point.Sub(prev))
		totals = append(totals, point.Sub(w.start))

		prev = point
	}

	return diffs, totals
}

//
// PrintResults output results.
//
func (w *StopWatch) PrintResults() {
	diffs, totals := w.Results()

	for index := range diffs {
		fmt.Printf("diff:%s total:%s \n", diffs[index], totals[index])
	}
}

func testCmd() *cobra.Command {
	rootCmd := &cobra.Command{
		Use:   "test",
		Short: "test command.",
		RunE: func(cmd *cobra.Command, args []string) error {
			return nil
		},
	}

	rootCmd.AddCommand(
		testStressCmd(),
	)

	return rootCmd
}
