package main

import (
	"bufio"
	"encoding/csv"
	"flag"
	"fmt"
	"html/template"
	"io"
	"os"
)

func main() {
	var file = flag.String("file", "", "File to import")
	var columnStart = flag.Int("columnStart", -1, "Column number with a start Datetime")
	var columnEnd = flag.Int("columnEnd", -1, "Column number with a end Datetime")
	var columnDescription = flag.Int("columnDescription", -1, "Column number with a descriptive name or similar")
	flag.Parse()

	if _, err := os.Stat(*file); os.IsNotExist(err) || *columnStart == -1 || *columnEnd == -1 || *columnDescription == -1 {
		flag.Usage()
		return
	}

	fmt.Println("Scanning file " + *file + "...")
	readCsvFile(*file, *columnStart, *columnEnd, *columnDescription)
}

func readCsvFile(csvFile string, columnStart int, columnEnd int, columnDescription int) {
	f, _ := os.Open(csvFile)
	r := csv.NewReader(bufio.NewReader(f))

	type JobDetails struct {
		Start       string
		End         string
		Description string
	}

	type ViewData struct {
		Jobs []JobDetails
	}

	var data ViewData

	for {
		row, err := r.Read()
		if err == io.EOF {
			break
		}

		job := JobDetails{
			Start:       row[columnStart],
			End:         row[columnEnd],
			Description: row[columnDescription],
		}

		data.Jobs = append(data.Jobs, job)
	}

	tpl, err := template.ParseFiles("template.html")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("%+v\n", data)
	tpl.Execute(os.Stdout, data)
}
