package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
)

// A wiki consists of a series of interconnected pages, each of which has a title and a body
type Page struct {
	Title string
	//The Body element is a []byte rather than string because that is the type expected by the io libraries we will use
	Body []byte
}

// save Saves a page to disk.
func (p *Page) save() error {
	filename := p.Title + ".txt"
	return os.WriteFile(filename, p.Body, 0600)
}

// loadPage loads a Page from disk.
// It takes the title of the page as an argument, reads the corresponding
// file, and returns a pointer to a Page struct containing the title and body.
// If an error occurs during reading, it returns the error.
func loadPage(title string) (*Page, error) {
	filename := title + ".txt"
	body, err := os.ReadFile(filename)

	if err != nil {
		return nil, err
	}
	return &Page{Title: title, Body: body}, nil
}

func viewHandler(w http.ResponseWriter, r *http.Request) {
	html := "<h1>%s</h1><div>%s</div>"
	filename := r.URL.Path[len("/view/"):]
	page, _ := loadPage(filename)
	fmt.Fprintf(w, html, page.Title, page.Body)
}

func editHandler(w http.ResponseWriter, r *http.Request) {
	title := r.URL.Path[len("/edit/"):]
	page, err := loadPage(title)
	if err != nil {
		page = &Page{Title: title}
	}
	fmt.Fprintf(w, "<h1>Editing %s</h1>"+
		"<form action=\"/save/%s\" method=\"POST\">"+
		"<textarea name=\"body\">%s</textarea><br>"+
		"<input type=\"submit\" value=\"Save\">"+
		"</form>",
		page.Title, page.Title, page.Body)

}

func saveHandler(w http.ResponseWriter, r *http.Request) {

}

func main() {
	http.HandleFunc("/view/", viewHandler)
	http.HandleFunc("/edit/", editHandler)
	http.HandleFunc("/save/", saveHandler)

	log.Println("Serving on: http://0.0.0.0:5001")
	log.Fatal(http.ListenAndServe(":5001", nil))
}

// func main() {
// 	p1 := &Page{Title: "TestPage", Body: []byte("This is a test page.")}
// 	p1.save()
// 	p2, _ := loadPage("TestPage")
// 	fmt.Println(string(p2.Body))
// }
