// Copyright 2017 Marc-Antoine Ruel. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.

package internal

import (
	"html/template"
	"os"
	"time"

	"github.com/maruel/panicparse/stack"
)

func writeToHTML(html string, buckets []*stack.Bucket, needsEnv bool) error {
	m := template.FuncMap{
		"funcClass":           funcClass,
		"notoColorEmoji1F4A3": notoColorEmoji1F4A3,
	}
	if len(buckets) > 1 {
		m["routineClass"] = routineClass
	} else {
		m["routineClass"] = func(bucket *stack.Bucket) template.HTML { return "Routine" }
	}
	t, err := template.New("htmlTpl").Funcs(m).Parse(htmlTpl)
	if err != nil {
		return err
	}
	data := struct {
		Buckets  []*stack.Bucket
		Now      time.Time
		NeedsEnv bool
	}{buckets, time.Now().Truncate(time.Second), needsEnv}

	f, err := os.Create(html)
	if err != nil {
		return err
	}
	err1 := t.Execute(f, data)
	err2 := f.Close()
	if err1 != nil {
		return err1
	}
	return err2
}

func funcClass(line *stack.Call) template.HTML {
	if line.IsStdlib {
		if line.Func.IsExported() {
			return "FuncStdLibExported"
		}
		return "FuncStdLib"
	} else if line.IsPkgMain() {
		return "FuncMain"
	} else if line.Func.IsExported() {
		return "FuncOtherExported"
	}
	return "FuncOther"
}

func routineClass(bucket *stack.Bucket) template.HTML {
	if bucket.First {
		return "RoutineFirst"
	}
	return "Routine"
}

const htmlTpl = `<!DOCTYPE html>

{{- define "RenderCall" -}}
{{.SrcLine}} <span class="{{funcClass .}}">{{.Func.Name}}</span>({{.Args}})
{{- end -}}

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PanicParse</title>
<link rel="shortcut icon" type="image/png" href="data:image/png;base64,{{notoColorEmoji1F4A3}}"/>
<style>
	body {
		background: black;
		color: lightgray;
	}
	body, pre {
		font-family: Menlo, monospace;
		font-weight: bold;
	}
	.FuncStdLibExported {
		color: #7CFC00;
	}
	.FuncStdLib {
		color: #008000;
	}
	.FuncMain {
		color: #C0C000;
	}
	.FuncOtherExported {
		color: #FF0000;
	}
	.FuncOther {
		color: #A00000;
	}
	.RoutineFirst {
	}
	.Routine {
	}
</style>
<div id="legend">Generated on {{.Now.String}}.
{{if .NeedsEnv}}
<br>To see all goroutines, visit <a
href=https://github.com/maruel/panicparse#gotraceback>github.com/maruel/panicparse</a>.<br>
{{end}}
</div>
<div id="content">
{{range .Buckets}}
	<h1>{{if .First}}Panicking {{end}}Routine</h1>
	<span class="{{routineClass .}}">{{len .IDs}}: <span class="state">{{.State}}</span>
	{{if .SleepMax -}}
	  {{- if ne .SleepMin .SleepMax}} <span class="sleep">[{{.SleepMin}}~{{.SleepMax}} minutes]</span>
		{{- else}} <span class="sleep">[{{.SleepMax}} minutes]</span>
		{{- end -}}
	{{- end}}
	{{if .Locked}} <span class="locked">[locked]</span>
	{{- end -}}
	{{- if .CreatedBy.SrcPath}} <span class="created">[Created by {{template "RenderCall" .CreatedBy}}]</span>
	{{- end -}}
	<h2>Stack</h2>
	{{range .Signature.Stack.Calls}}
	- {{template "RenderCall" .}}<br>
	{{- end}}
	{{if .Stack.Elided}}(...)<br>{{end}}
{{end}}
</div>
`

// notoColorEmoji1F4A3 is the bomb emoji U+1F4A3 in Noto Emoji as a PNG.
//
// Source: https://www.google.com/get/noto/help/emoji/smileys-people.html
// License: http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL
//
// Created with:
//   python -c "import base64;a=base64.b64encode(open('emoji_u1f4a3.png','rb').read()); print '\n'.join(a[i:i+70] for i in range(0,len(a),70))"
func notoColorEmoji1F4A3() template.HTML {
	return "" +
		"iVBORw0KGgoAAAANSUhEUgAAAIgAAACACAMAAADnN9ENAAAA5FBMVEVMaXFvTjQhISEhIS" +
		"EhISEhISEhISEhISEhISEhISEhISEhISHRbBTRbBTRbBQhISHRbBTRbBTRbBTRbBQ6OjrR" +
		"bBTZcBTsehbzfhf1fxfidRVOTk75oiL7uCr90zL3kB3/6zv2hhlHMR5OTk5KSkpKSkpRUV" +
		"FKSkpKSkpMTExBQUE2NjYpKSlOTk5EREQ8PDw4ODgtLS1RUVFQUFAmJiYkJCQ0NDRLS0tT" +
		"U1MoKCgyMjJHR0dTU1NdXV0rKytXV1dVVVUvLy9eXl5aWlpcXFxcXFxeXl5fX186OjphYW" +
		"E/Pz9KSkrdB5CTAAAATHRSTlMAEUZggJjf/6/vcL86e1nP2//vmSC7//////9E////////" +
		"/2WPpYHp////////////////////z/////8w6P////+p/8f///////+/QBb3BQAACWJJRE" +
		"FUeAHslgWC6yAURUOk1N3d3d29+9/SzzxCStvQEfp9zgZycu8DnvTNN78YJCuqqsry77WQ" +
		"NRum2BX0u7JQiYWJw/l7NBz4AderQkFut8frdn+kFBu2wodeYeHxBwjBkFd6StiFOYiboF" +
		"CAxf9MRXHc9KGpqurDBpqghzcYuCOCeMp21oKelTBVCQt5kDiisXhCJ5aMQkFu6+lg4tDY" +
		"r2rikRCPKFgQYlGeiYpN7OHbpMj8OgQ8PAGdWIIlnnwbFKYdtgDAJj+MDgZSX/Zwsx4mby" +
		"YR6RbntRZVegQDX7/tI1YexMTNObQ+y992EUWRQJIJQjqTzWRyRjtRiMTqKtWQJCTCD4TM" +
		"aS6bBzLGxLKRKNer1MELX0wEmYHk8pSMGUmIlMI+LC7uTeEQmhEvnZBC/kqGTolfkmSn72" +
		"NPbFhoWOHswmczeYYC7YaV4MfBHl+BEYmCSJYVSUM3ukgRM5C7g4edqAqLMBq0m1sRh8oe" +
		"Fl4zzp8swtFApXKlmkIkECD8+mpYEZ8iWZGq1YFKKazg582IDiu8bk7Ob5YaOnVCs9UWu+" +
		"C99D7LWR5fVeY/YpVOp9Po9rp1g25/AIGIXmhp0yMLgSTIhcYDVYaj0ag1Hk/a0yZ1qVVK" +
		"6KsmfnMVSbMepBkv32M2nw87rfZioatMxsveirqUBLoxHr1CJpvNZmBQSSB+icd6M9dFpt" +
		"tt21CZ4EHfKKny9Ug4awA/kNRmt993loPBAFSICcbtFpRUeuViFIPFiENpp9NYHg6HexW8" +
		"1Suaia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