package cache

import  (
	"errors"
	. "openreplay/backend/pkg/messages"
	. "openreplay/backend/pkg/db/types"
)


func (c *PGCache) InsertWebSessionStart(sessionID uint64, s *SessionStart) error {
	if c.sessions[ sessionID ] != nil {
		return errors.New("This session already in cache!")
	}
	c.sessions[ sessionID ] = &Session{
		SessionID: sessionID,
		Platform: "web",
		Timestamp: s.Timestamp,
		ProjectID: uint32(s.ProjectID),
		TrackerVersion: s.TrackerVersion,
		RevID: s.RevID,
		UserUUID: s.UserUUID,
		UserOS: s.UserOS,
		UserOSVersion: s.UserOSVersion,
		UserDevice: s.UserDevice,
		UserCountry: s.UserCountry,
		// web properties (TODO: unite different platform types)
		UserAgent: s.UserAgent,
		UserBrowser: s.UserBrowser,
		UserBrowserVersion: s.UserBrowserVersion,
		UserDeviceType: s.UserDeviceType,
		UserDeviceMemorySize: s.UserDeviceMemorySize,
		UserDeviceHeapSize: s.UserDeviceHeapSize,
	}
	if err := c.Conn.InsertSessionStart(sessionID, c.sessions[ sessionID ]); err != nil { 
		c.sessions[ sessionID ] = nil
		return err
	}
	return nil;
}

func (c *PGCache) InsertWebSessionEnd(sessionID uint64, e *SessionEnd) error {
	return c.insertSessionEnd(sessionID, e.Timestamp)
}

func (c *PGCache) InsertWebErrorEvent(sessionID uint64, e *ErrorEvent) error {
	session, err := c.GetSession(sessionID)
	if err != nil {
		return err
	}
	if err := c.Conn.InsertWebErrorEvent(sessionID, session.ProjectID, e); err != nil {
		return err
	}
	session.ErrorsCount += 1
	return nil
}

