package domain

import "fmt"

type Folder struct {
	Title      string // k
	PathTo     string // /temp/k
	PrivateURL string // http://domain/apps/files/?dir=/temp/k OR  http://domain/f/3333
}

type CodaApplication struct {
	ID       string `json:"id"`
	Summary  string `json:"summary"`
	URL      string `json:"url"`
	Git      string `json:"git"`
	GitBuild string `json:"git-build"`
	Folder   string `json:"folder"`
}

type Insert struct {
	Rows []Row `json:"rows"`
}

func NewTaskRequest() *Insert {
	return &Insert{}
}

type Row struct {
	Cells []Cell `json:"cells"`
}

func (req *Insert) NewRow() *Row {
	row := Row{}
	i := len(req.Rows)
	req.Rows = append(req.Rows, row)
	return &req.Rows[i]
}

type Cell struct {
	Column string `json:"column"`
	Value  string `json:"value"`
}

func (r *Row) NewCell(col string, value string) *Row {
	cell := Cell{
		Column: col,
		Value:  value,
	}
	r.Cells = append(r.Cells, cell)
	return r
}

type Task struct {
	Summary     string
	Description string
	Creator     string
	CreatorLink string

	URL string
}

func NewTask(summ, desc, c, cLink string) *Task {
	return &Task{
		Summary:     summ,
		Description: desc,
		Creator:     c,
		CreatorLink: cLink,
	}
}

type ConversionLog struct {
	Advertiser []string
}

type Git struct {
	Name     string `json:"name"`      // "poop"
	FullName string `json:"full_name"` // "developer/poop"
	Private  bool   `json:"private"`
	Url      string `json:"url"`       // "http://localhost:8081/api/v3/repos/developer/poop"
	CloneUrl string `json:"clone_url"` // "http://localhost:8081/git/developer/poop.git"
	HtmlUrl  string `json:"Html_url"`  // "http://localhost:8081/developer/poop"
	SshUrl   string `json:"ssh_url"`   // ?!
}

type Project struct {
	ID        string `json:"id"`
	ShortName string `json:"shortName"`
	Name      string `json:"name"`
}

type ProjectID struct {
	ID string `json:"id"`
}

type IssueCreateRequest struct {
	ProjectID   ProjectID `json:"project"`
	Key         string    `json:"idReadable"`
	ID          string    `json:"id"`
	Summary     string    `json:"summary"`
	Description string    `json:"description"`
}

// [ ] try `,omitempty` to remove extra struct;

type IssueUpdateRequest struct {
	IssueCreateRequest
	CustomFields []CustomField `json:"customFields"`
}

type CustomField struct {
	Name  string `json:"name"`
	Type  string `json:"$type"`
	Value string `json:"value"`
}

type ProjectsList struct {
	Projects []Project
}

// Find needed project.ID in the project's list
func (plist *ProjectsList) FindProjectByName(searchName string) (string, error) {

	projectID := ""

	for _, elem := range plist.Projects {
		if elem.ShortName == searchName {
			projectID = elem.ID
		}
	}

	if projectID == "" {
		return "", fmt.Errorf("project %s doesn't exist", searchName)
	}
	return projectID, nil
}
