package domain

import (
	"fmt"
	"strings"
	"time"
)

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 Response struct {
	Folder     *Folder
	ErrMessage error
}

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 {
	ID          int32
	Summary     string
	Description string
	Creator     string
	CreatorLink string

	Assignee string

	CreatedAt time.Time
	DeletedAt time.Time
	UpdatedAt time.Time

	URL string
}

type TaskState int

const (
	new TaskState = iota
	inprogress
	done
)

func State(i int) TaskState {
	switch i {
	case 0:
		return new
	case 1:
		return inprogress
	case 2:
		return done
	}
	return -1
}

func NewTaskState() TaskState {
	return TaskState(0)
}

func InrpogressTaskState() TaskState {
	return TaskState(1)
}

func DoneTaskState() TaskState {
	return TaskState(2)
}

// Creates a string for discordgo.DiscordMessage.Content
// State: New task;
func (t *Task) DiscordMessage(ts TaskState) string {
	switch ts {
	case new:
		return fmt.Sprintf(
			"Created at: %s \n>>> %s\n",
			t.CreatedAt,
			t.Description,
		)
	case inprogress:
		return fmt.Sprintf(
			"**TaskID: %d** Started by: %s\n🚀 Started at: %s\n",
			t.ID,
			t.Assignee,
			t.UpdatedAt,
		)
	case done:
		return fmt.Sprintf(
			"**TaskID: %d** Closed by: %s\n✅ Closed at: %s\n",
			t.ID,
			t.Assignee,
			t.DeletedAt,
		)
	}
	return "task state not provided"
}

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

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"`         //15
	Key       string `json:"shortName"`  //key-15
	Name      string `json:"name"`       //default project name
	ChannelID string `json:"channel_id"` //123412341234

	ProjectGit string `json:"project_git"` //https://github.com/mobilerino/dap-108
	BuildGit   string `json:"build_git"`   //https://github.com/mobilerino/dap-108-build
	Folder     string `json:"cloud"`       //http://82.151.222.22:7000/f/86658
}

func (p *Project) DiscordString() string {

	var builder strings.Builder
	builder.WriteString(fmt.Sprintf("## Project `%s`:\n> 🔑 key: %s", p.Name, p.Key))

	if p.Folder != "" {
		builder.WriteString(fmt.Sprintf("\n> 📂 folder: %s", p.Folder))
	}

	if p.ProjectGit != "" {
		builder.WriteString(fmt.Sprintf("\n> 👾 project git: %s", p.ProjectGit))
	}

	if p.BuildGit != "" {
		builder.WriteString(fmt.Sprintf("\n> 🚀 build git: %s", p.BuildGit))
	}

	return builder.String()
}
