package rest_websocket

import (
	"log"

	"git.ali33.ru/fcg-xvii/go-tools/json"
	"git.ali33.ru/fcg-xvii/rest"
)

/*
type IRequest interface {
	Type() RequestType
	IsAuth() bool
	Command() string
	Auth() json.Map
	Data() json.Map
	FileKeys() []string
	File(name string) (IReadCloserLen, bool)
	GenerateToken(data json.Map, expire int64) (string, error)
	Root() any
	Core() any
	ResponseSuccess(data json.Map, files map[string]IReadCloserLen) IResponse
	ResponseError(code int, err IErrorArgs) IResponse
	Close()
}
)*/

type Request struct {
	mes *Message
}

func (s *Request) Type() rest.RequestType {
	return s.mes.mType
}

func (s *Request) IsAuth() bool {
	return s.mes.owner.Auth != nil
}

func (s *Request) Auth() json.Map {
	return s.mes.owner.Auth
}

func (s *Request) SetAuth(m json.Map) {
	s.mes.owner.Auth = m
}

func (s *Request) Command() string {
	return s.mes.command
}

func (s *Request) Data() json.Map {
	return s.mes.data
}

func (s *Request) FileKeys() []string {
	return s.mes.FileKeys()
}

func (s *Request) File(name string) (rest.IReadCloserLen, bool) {
	res, check := s.mes.files[name]
	return res, check
}

func (s *Request) GenerateToken(data json.Map, expire int64) (string, error) {
	return s.mes.owner.appConf.tokenGenerator(data, expire)
}

func (s *Request) Root() any {
	return s.mes.owner
}

func (s *Request) Core() any {
	return s.mes.owner.appConf.core
}

func (s *Request) ResponseSuccess(data json.Map, files map[string]rest.IReadCloserLen) rest.IResponse {
	if s.mes.mType == rest.RequestTypeEvent {
		return &rest.ResponseEmpty{}
	}
	return &Response{
		mes:   s.mes,
		data:  data,
		files: files,
	}
}

func (s *Request) ResponseError(code int, err rest.IErrorArgs) rest.IResponse {
	log.Println("RESP-ERROR", err.Map())
	if s.mes.mType == rest.RequestTypeEvent {
		return &rest.ResponseEmpty{}
	}
	return &Response{
		mes: s.mes,
		err: err,
	}
}

func (s *Request) Close() {
	s.mes.Close()
}
