package compiler

import (
	"io/ioutil"
	"testing"

	"github.com/remogatto/cloud"
	"github.com/remogatto/prettytest"

	"gogs.carducci-dante.gov.it/karmen/config"
)

type testSuite struct {
	prettytest.Suite
}

var (
	compiler   *Compiler
	client     *cloud.Client
	simpleMain string = `
package main

func main() {
  println("Hello World!")
}

`
	cloudTemplateProgram string = `
package main

import (
	"bytes"
	"fmt"
	"text/template"

	"github.com/remogatto/cloud"
        "gogs.carducci-dante.gov.it/karmen/config"
)

func main() {
	var (
		WebDAVUrl      = "http://karmen_cloud_test/remote.php/webdav/"
		WebDavUsername = "admin"
		WebDAVPassword = "password"
                config         config.ConfigT
		data           struct{ Data string }
		output         bytes.Buffer
	)
	content, err := client.Download("Config/config.yaml")
	if err != nil {
		fmt.Print(err)
	}
        if err := config.Read(configData, &config); err != nil {
                fmt.Print(err)
        }
	client, err := cloud.Dial(config.Cloud.Url, config.Cloud.Username, config.Cloud.Password)
	if err != nil {
		fmt.Print(err)
	}
	content, err := client.Download("Templates/template.tpl")
	if err != nil {
		fmt.Print(err)
	}
	tpl, err := template.New("template.tpl").Parse(string(content))
	if err != nil {
		fmt.Print(err)
	}
	data.Data = "Foo"
	err = tpl.Execute(&output, data)
	if err != nil {
		fmt.Print(err)
	}

	err = client.Mkdir("Test")
	if err != nil {
		fmt.Print(err)
	}

	err = client.Upload(output.Bytes(), "Test/foo.txt")
	if err != nil {
		fmt.Print(err)
	}
        fmt.Print("ok")
}
`
)

func TestRunner(t *testing.T) {
	prettytest.Run(
		t,
		new(testSuite),
	)
}

func (t *testSuite) BeforeAll() {
	var err error
	compiler = New("http://localhost:8080/compile")
	client, err = cloud.Dial(
		config.WebDAVUrl,
		config.WebDavUsername,
		config.WebDAVPassword,
	)
	if err != nil {
		panic(err)
	}
	if !client.Exists("Templates") {
		if err := client.Mkdir("Templates"); err != nil {
			panic(err)
		}
	}
	templateData, err := ioutil.ReadFile("testdata/template.tpl")
	if err != nil {
		panic(err)
	}
	if err := client.Upload(templateData, "Templates/template.tpl"); err != nil {
		panic(err)
	}

	if !client.Exists("Config") {
		if err := client.Mkdir("Config"); err != nil {
			panic(err)
		}
	}
	configData, err := ioutil.ReadFile("testdata/config.yaml")
	if err != nil {
		panic(err)
	}
	if err := client.Upload(configData, "Config/config.yaml"); err != nil {
		panic(err)
	}

}

func (t *testSuite) TestRunSimpleMain() {
	program := &Program{simpleMain}
	result, err := compiler.Run(program)
	t.Nil(err)
	if err == nil {
		t.Equal("Hello World!\n", result.Events[0].Message)
	}
}

func (t *testSuite) TestCloudTemplate() {
	program := &Program{cloudTemplateProgram}
	result, err := compiler.Run(program)
	t.Nil(err)
	if err == nil {
		t.Equal("", result.Errors)
		if result.Errors == "" {
			t.Equal("ok", result.Events[0].Message)
			data, err := client.Download("Test/foo.txt")
			t.Nil(err)
			t.Equal("Foo", string(data))
		}
	}

}

func (t *testSuite) After() {
	client.Delete("Test")
}
