package mitre

import (
	"context"
	"time"

	"github.com/TcM1911/stix2"
	"github.com/dstotijn/go-notion"
)

const (
	// malwareDatabaseTitle is the title of the malware database.
	malwareDatabaseTitle = "MITRE ATT&CK - Malware"
	// malwareDatabaseIcon is the emoji icon for the malware database.
	malwareDatabaseIcon = "🦠"
	// malwarePageIcon is the emoji icon for the malware page.
	malwarePageIcon = "🦠"
)

// MALWARE_PROPERTIES defines the properties of the malware database.
// var MALWARE_PROPERTIES =
// ListMalware returns all the malware objects in the collection.
func (m *MITRE) ListMalware() []*stix2.Malware {
	return m.Collection.AllMalware()
}

// malwareByID returns the malware object with the specified ID.
func (m *MITRE) malwareByID(id string) *stix2.Malware {
	return m.Collection.Malware(stix2.Identifier(id))
}

// CreateMalwareDatabase creates a malware database in Notion.
func (m *MITRE) CreateMalwareDatabase(ctx context.Context, client *notion.Client, parentPageID string) (notion.Database, error) {
	params := notion.CreateDatabaseParams{
		ParentPageID: parentPageID,
		Title:        []notion.RichText{{Text: &notion.Text{Content: malwareDatabaseTitle}}},
		Properties: notion.DatabaseProperties{
			"Name": {
				Type:  notion.DBPropTypeTitle,
				Title: &notion.EmptyMetadata{},
			},
			"Description": {
				Type:     notion.DBPropTypeRichText,
				RichText: &notion.EmptyMetadata{},
			},
			"Created": {
				Type: notion.DBPropTypeDate,
				Date: &notion.EmptyMetadata{},
			},
			"Imported": {
				Type: notion.DBPropTypeDate,
				Date: &notion.EmptyMetadata{},
			},
		},
		Icon: &notion.Icon{
			Type:  notion.IconTypeEmoji,
			Emoji: notion.StringPtr(malwareDatabaseIcon),
		},
	}

	m.Logger.Info("Creating Notion database", "title", malwareDatabaseTitle)
	return client.CreateDatabase(ctx, params)
}

func (m *MITRE) CreateMalwarePage(ctx context.Context, client *notion.Client, db notion.Database, malware *stix2.Malware) (notion.Page, error) {

	var blocks []notion.Block

	blocks = append(blocks, []notion.Block{
		notion.Heading2Block{
			RichText: []notion.RichText{{Type: notion.RichTextTypeText, Text: &notion.Text{Content: "Capabilities"}}},
		},
	}...)

	blocks = append(blocks, capabilitiesToBlocks(malware.Capabilities)...)

	blocks = append(blocks, []notion.Block{
		notion.Heading2Block{
			RichText: []notion.RichText{{Type: notion.RichTextTypeText, Text: &notion.Text{Content: "References"}}},
		},
	}...)

	blocks = append(blocks, referencesToBlocks(malware.ExternalReferences)...)

	properties := notion.CreatePageParams{
		ParentID:   db.ID,
		ParentType: notion.ParentTypeDatabase,
		Icon: &notion.Icon{
			Type:  notion.IconTypeEmoji,
			Emoji: notion.StringPtr(malwarePageIcon),
		},
		Title: []notion.RichText{
			{
				Text: &notion.Text{
					Content: malware.Name,
				},
			},
		},
		Children: blocks,
		DatabasePageProperties: &notion.DatabasePageProperties{
			"Name": notion.DatabasePageProperty{
				Type: notion.DBPropTypeTitle,
				Title: []notion.RichText{
					{
						Type: notion.RichTextTypeText,
						Text: &notion.Text{
							Content: malware.Name,
						},
					},
				},
			},
			"Description": notion.DatabasePageProperty{
				Type: notion.DBPropTypeRichText,
				RichText: []notion.RichText{
					{
						Type: notion.RichTextTypeText,
						Text: &notion.Text{
							Content: limitString(malware.Description, 2000),
						},
					},
				},
			},
			"Imported": notion.DatabasePageProperty{
				Type: notion.DBPropTypeDate,
				Date: &notion.Date{
					Start: notion.NewDateTime(time.Now(), false),
				},
			},
			"Created": notion.DatabasePageProperty{
				Type: notion.DBPropTypeDate,
				Date: &notion.Date{
					Start: notion.NewDateTime(malware.Created.Time, false),
				},
			},
		},
	}
	m.Logger.Debug("Creating page", "name", malware.Name, "type", "malware")
	return client.CreatePage(ctx, properties)
}
