package main

import (
	"bufio"
	"net/http"

	"github.com/juju/errors"
	"github.com/pingcap/schrodinger/pkg/test_client"
	"github.com/pingcap/schrodinger/pkg/types"
)

// Version is version
type Version struct {
	Tidb types.VersionInfo `json:"tidb"`
	Tikv types.VersionInfo `json:"tikv"`
	Pd   types.VersionInfo `json:"pd"`
}

// Pair is pair version
type Pair struct {
	Source string `json:"source"`
	Target string `json:"target"`
	Status string `json:"status"`
	Errors string `json:"error"`
}

func getTargetCatVersion(cli *client.Client) *Version {
	config := cli.CatConfig()
	return &Version{
		Tidb: config.TiDBConfig.Version,
		Tikv: config.TiKVConfig.Version,
		Pd:   config.PDConfig.Version,
	}
}

func getSourceCatVersions() []*Version {
	var versions []*Version
	v1 := &Version{
		Tidb: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.1",
			Platform: "",
		},
		Tikv: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.1",
			Platform: "",
		},
		Pd: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.1",
			Platform: "",
		},
	}
	v2 := &Version{
		Tidb: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.2",
			Platform: "",
		},
		Tikv: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.2",
			Platform: "",
		},
		Pd: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.2",
			Platform: "",
		},
	}
	v3 := &Version{
		Tidb: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.3",
			Platform: "",
		},
		Tikv: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.3",
			Platform: "",
		},
		Pd: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.3",
			Platform: "",
		},
	}

	v4 := &Version{
		Tidb: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.4",
			Platform: "",
		},
		Tikv: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.4",
			Platform: "",
		},
		Pd: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.4",
			Platform: "",
		},
	}

	v5 := &Version{
		Tidb: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.5",
			Platform: "",
		},
		Tikv: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.5",
			Platform: "",
		},
		Pd: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.5",
			Platform: "",
		},
	}

	v6 := &Version{
		Tidb: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.6",
			Platform: "",
		},
		Tikv: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.6",
			Platform: "",
		},
		Pd: types.VersionInfo{
			Type:     "tag",
			Value:    "v2.0.6",
			Platform: "",
		},
	}

	versions = append(versions, v1)
	versions = append(versions, v2)
	versions = append(versions, v3)
	versions = append(versions, v4)
	versions = append(versions, v5)
	versions = append(versions, v6)
	return versions
}

func buildVersion(version string) *Version {
	return &Version{
		Tidb: types.VersionInfo{
			Type:     "tag",
			Value:    version,
			Platform: "",
		},
		Tikv: types.VersionInfo{
			Type:     "tag",
			Value:    version,
			Platform: "",
		},
		Pd: types.VersionInfo{
			Type:     "tag",
			Value:    version,
			Platform: "",
		},
	}
}

func getVersions() ([]string, error) {
	url := "http://pingcap-dev.hk.ufileos.com/schrodinger/upgrade.txt"
	resp, err := http.Get(url)
	if err != nil {
		return nil, errors.Trace(err)
	}
	defer resp.Body.Close()

	var ret []string
	reader := bufio.NewReader(resp.Body)
	for {
		line, err := reader.ReadBytes('\n')
		if err != nil {
			return ret, nil
		}
		ret = append(ret, string(line))
	}
}
