// package router_anonym -- реализация HTTP-роутера с анонимным хранилищем
package router_anonym

import (
	"fmt"
	"time"

	"github.com/gofiber/fiber/v2"

	"git.p78su.freemyip.com/svi/gostore/pkg/serv_http/router_anonym/anonym_del"
	"git.p78su.freemyip.com/svi/gostore/pkg/serv_http/router_anonym/anonym_find"
	"git.p78su.freemyip.com/svi/gostore/pkg/serv_http/router_anonym/anonym_list"
	"git.p78su.freemyip.com/svi/gostore/pkg/serv_http/router_anonym/anonym_put"
	"git.p78su.freemyip.com/svi/gostore/pkg/serv_http/router_anonym/anonym_read"
	"git.p78su.freemyip.com/svi/gostore/pkg/types"
)

// RouterAnonym -- реализация HTTP-роутера с анонимным хранилищем
type RouterAnonym struct {
	*anonym_put.AnonymPut
	*anonym_read.AnonymRead
	*anonym_find.AnonymFind
	*anonym_del.AnonymDel
	*anonym_list.AnonymList
	serv     types.IService
	servHttp types.IServHttp
	router   fiber.Router
}

// NewRouterAnonym -- возвращает новый HTTP-роутер с анонимным хранилищем
func NewRouterAnonym(servHttp types.IServHttp) (*RouterAnonym, error) {
	if servHttp == nil {
		return nil, fmt.Errorf("NewRouterAnonym(): IServHttp==nil")
	}
	serv := servHttp.Service()
	sf := &RouterAnonym{
		serv:     serv,
		servHttp: servHttp,
		router:   servHttp.FiberApp().Group("/anonym"),
	}
	sf.router.Get("/", sf.get)
	var err error
	sf.AnonymPut, err = anonym_put.NewAnonymPut(servHttp, sf.router)
	if err != nil {
		return nil, fmt.Errorf("NewRouterAnonym(): in create AnonymPut, err=\n\t%w", err)
	}
	sf.AnonymRead, err = anonym_read.NewAnonymRead(servHttp, sf.router)
	if err != nil {
		return nil, fmt.Errorf("NewRouterAnonym(): in create AnonymRead, err=\n\t%w", err)
	}
	sf.AnonymFind, err = anonym_find.NewAnonymFind(servHttp, sf.router)
	if err != nil {
		return nil, fmt.Errorf("NewRouterAnonym(): in create AnonymFind, err=\n\t%w", err)
	}
	sf.AnonymDel, err = anonym_del.NewAnonymDel(servHttp, sf.router)
	if err != nil {
		return nil, fmt.Errorf("NewRouterAnonym(): in create AnonymDel, err=\n\t%w", err)
	}
	sf.AnonymList, err = anonym_list.NewAnonymList(servHttp, sf.router)
	if err != nil {
		return nil, fmt.Errorf("NewRouterAnonym(): in create AnonymList, err=\n\t%w", err)
	}
	sf.router.Post("/time", sf.time)
	return sf, nil
}

// Возвращает страницу анонимного раздела
func (sf *RouterAnonym) get(c *fiber.Ctx) error {
	return c.Render("anonym_put", fiber.Map{})
}

// Возвращает текущее время
func (sf *RouterAnonym) time(c *fiber.Ctx) error {
	strTime := time.Now().Local().Format("2006-01-02 15:04:05 MST -07")
	return c.SendString(strTime)
}
