package message

import (
	"errors"
	"strings"
)


type RequestExecUnitTestRecommend struct {
	RequestExec
	FileContent   string    `json:"file_content"`
	FilePath      string    `json:"filepath"`
	FuncName      string    `json:"func_name"`
	FuncLocation  TextRange `json:"func_location"`

	FuncSymbols   Symbol    `json:"func_symbols"`

	lines []string
}

func (req *RequestExecUnitTestRecommend) splitLines() {
	if req.lines == nil {
		req.lines = strings.Split(req.FileContent, "\n")
	}
}


func (req *RequestExecUnitTestRecommend) GetContent(loc *TextRange) (string, error) {
	req.splitLines()
	ln := len(req.lines)
	if ln >= loc.Start.Line || ln >= loc.End.Line {
		return "", errors.New("invalid format")
	}
	if len(req.lines[loc.Start.Line]) >= loc.Start.Character || len(req.lines[loc.End.Line]) >= loc.End.Character {
		return "", errors.New("invalid format")
	}

	if loc.Start.Line == loc.End.Line {
		return req.lines[loc.Start.Line][loc.Start.Character:loc.End.Character], nil
	}

	var lines []string
	for i:=loc.Start.Line; i<=loc.End.Line; i++ {
		if i == loc.Start.Line {
			lines = append(lines, req.lines[i][loc.Start.Character:])
		} else if i == loc.End.Line {
			lines = append(lines, req.lines[i][:loc.End.Character])
		} else {
			lines = append(lines, req.lines[i])
		}
	}
	return strings.Join(lines, "\n"), nil
}

