package svc

import (
	"errors"
	"github.com/globalsign/mgo"
	"github.com/globalsign/mgo/bson"
	"mongo.games.com/game/dbproxy/mongo"
	"mongo.games.com/game/model"
	"mongo.games.com/goserver/core/logger"
	"net/rpc"
)

var (
	FriendUnreadDBName   = "log"
	FriendUnreadCollName = "log_friendunread"
	FriendUnreadColError = errors.New("friendunread collection open failed")
)

func FriendUnreadCollection(plt string) *mongo.Collection {
	s := mongo.MgoSessionMgrSington.GetPltMgoSession(plt, FriendUnreadDBName)
	if s != nil {
		c, first := s.DB().C(FriendUnreadCollName)
		if first {
			c.EnsureIndex(mgo.Index{Key: []string{"snid"}, Background: true, Sparse: true})
		}
		return c
	}
	return nil
}

type FriendUnreadSvc struct {
}

func (svc *FriendUnreadSvc) UpsertFriendUnread(args *model.FriendUnreadByKey, ret *model.FriendUnreadRet) error {
	cc := FriendUnreadCollection(args.Platform)
	if cc == nil {
		return FriendUnreadColError
	}
	err := cc.Find(bson.M{"snid": args.SnId}).One(&ret.FU)
	if err != nil && err != mgo.ErrNotFound {
		logger.Logger.Error("UpsertFriendUnread Find is err: ", err)
		return err
	}
	if err == mgo.ErrNotFound {
		fu := model.NewFriendUnread(args.SnId)
		fu.UnreadSnids = append(fu.UnreadSnids, &model.FriendUnreadSnid{
			SnId:      args.UnreadSnid,
			UnreadNum: 1,
		})
		err = cc.Insert(fu)
	} else {
		had := false
		for _, unreadSnids := range ret.FU.UnreadSnids {
			if unreadSnids.SnId == args.UnreadSnid {
				unreadSnids.UnreadNum++
				had = true
			}
		}
		if !had {
			ret.FU.UnreadSnids = append(ret.FU.UnreadSnids, &model.FriendUnreadSnid{
				SnId:      args.UnreadSnid,
				UnreadNum: 1,
			})
		}
		err = cc.Update(bson.M{"snid": args.SnId}, bson.M{"$set": bson.M{"unreadsnids": ret.FU.UnreadSnids}})
	}
	if err != nil {
		logger.Logger.Error("UpsertFriendUnread is err: ", err)
		return err
	}
	return nil
}

func (svc *FriendUnreadSvc) UpdateFriendUnread(args *model.FriendUnreadByKey, ret *model.FriendUnreadRet) error {
	cc := FriendUnreadCollection(args.Platform)
	if cc == nil {
		return FriendUnreadColError
	}
	err := cc.Find(bson.M{"snid": args.SnId}).One(&ret.FU)
	if err != nil && err != mgo.ErrNotFound {
		logger.Logger.Error("UpdateFriendUnread Find is err: ", err)
		return err
	}
	if err == mgo.ErrNotFound {
		err = cc.Insert(args.FU)
	} else {
		err = cc.Update(bson.M{"snid": args.SnId}, bson.M{"$set": bson.M{"unreadsnids": args.FU.UnreadSnids}})
	}
	if err != nil {
		logger.Logger.Error("UpdateFriendUnread is err: ", err)
		return err
	}
	return nil
}

func (svc *FriendUnreadSvc) QueryFriendUnreadByKey(args *model.FriendUnreadByKey, ret *model.FriendUnreadRet) error {
	fc := FriendUnreadCollection(args.Platform)
	if fc == nil {
		return FriendUnreadColError
	}
	err := fc.Find(bson.M{"snid": args.SnId}).One(&ret.FU)
	if err != nil && err != mgo.ErrNotFound {
		logger.Logger.Error("QueryFriendUnreadByKey is err: ", err)
		return err
	}
	return nil
}

func (svc *FriendUnreadSvc) DelFriendUnread(args *model.FriendUnreadByKey, ret *bool) error {
	cc := FriendUnreadCollection(args.Platform)
	if cc == nil {
		return FriendUnreadColError
	}
	err := cc.Remove(bson.M{"snid": args.SnId})
	if err != nil && err != mgo.ErrNotFound {
		logger.Logger.Error("DelFriendUnread is err: ", err)
		return err
	}
	return nil
}

var _FriendUnreadSvc = &FriendUnreadSvc{}

func init() {
	rpc.Register(_FriendUnreadSvc)
}
