package core

import (
	"fmt"
	"io"
	"os"

	"code.gitea.io/sdk/gitea"
)

type ByLineNum []*gitea.PullReviewComment

func (a ByLineNum) Len() int           { return len(a) }
func (a ByLineNum) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByLineNum) Less(i, j int) bool { return a[i].LineNum < a[j].LineNum }

func ExitOnError(msg string, err error) {
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: ", msg)
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func BackUpSource(filepath string) {
	inputFile, err := os.Open(filepath)
	ExitOnError("Couldn't open input file for backup", err)
	defer inputFile.Close()

	BackUpSourceFile, err := os.Create(filepath + ".bck")
	ExitOnError("Couldn't create backup file", err)
	defer BackUpSourceFile.Close()

	_, err = io.Copy(BackUpSourceFile, inputFile)
	ExitOnError("Couldn't copy into backup file", err)
}