package main

import (
	"fmt"
	"github.com/flamego/captcha"
	"github.com/flamego/flamego"
	"github.com/flamego/session"
	"github.com/flamego/template"
	user2 "kuvia/pkg/user"
	"net/http"
)

func userProfile(t template.Template, data template.Data, s session.Session, captcha captcha.Captcha, c flamego.Context) {


	user, err := user2.GetUserByUsername(sqlConnection, c.Param("name"))
	if err != nil {
		panic(err)
	}

	gallerys := []Gallery{}
	err = sqlConnection.Select(&gallerys, "SELECT * FROM gallery WHERE owner_id = ? ORDER BY id DESC", user.Id)
	if err != nil {
		panic(err)
	}

	var list [][]Gallery
	var tmp []Gallery

	for _, g := range gallerys {
		if len(tmp) == 3 {
			list = append(list, tmp)
			tmp = []Gallery{}
		}
		tmp = append(tmp, g)
	}
	list = append(list, tmp)

	data["list"] = list
	data["user"] = user
	fmt.Println(list)
	t.HTML(http.StatusOK, "user_profil")
}

func userGallery(t template.Template, data template.Data, c flamego.Context) {
	//todo: check if gallery belongs to user
	user, err := user2.GetUserByUsername(sqlConnection, c.Param("name"))
	if err != nil {
		panic(err)
	}

	images := []File{}
	sqlConnection.Select(&images, "SELECT * FROM images WHERE gallery_id = ?", c.Param("id"))
	data["images"] = images
	data["user"] = user
	t.HTML(http.StatusOK, "user_gallery")
}

