#!/usr/bin/env python
# -*- coding: utf-8 -*-

import EmailFlog
from Hero import _Hero
from Item import _Item
# message Email 
# {
#     repeated Item pHero = 1;                        //邮件奖励英雄
#     repeated HeroSkill pHeroSkill = 2;                //返回英雄技能
#     repeated Item pItems = 3;                        //邮件奖励道具
#     optional bytes pTitle = 4;                        //邮件标题
#     optional bytes pText = 5;                        //邮件内容
#     optional int32 emailID = 6;                        //用户邮件编号
#     optional int32 emailType = 7;                    //邮件类型,1,系统信息,2,玩客信息,3,任务信息,4,战报信息,5,帮战信息,6,GM信息,7.到付邮件（到付邮件在玩家交换物品时使用）8.到付邮件被退回
#     optional Resource res = 8;                        //邮件奖励资源
#     optional int32 lookType = 9;                    //邮件查看类型,1.邮件未被打开过,2.邮件已查看,但物品未领取,3.邮件已被查看且物品已领取
# }

#{'to':'toaccount','title','title','content','type',1,'items':'','heros':'','res':''}
class EmailData():
    def __init__(self,eType,eTitle,eContent,eNid = 0,eLookType = EmailFlog.flog_elTypeUnLook):
        self.eFrom = ''
        self.eTo = ''
        self.emailType = eType
        self.emailTitle = eTitle
        self.emailContent = eContent
        self.emailNID = eNid   #这个ID在创建时使用的是临时ID
        self.emailLookType = eLookType
        self.items = ''
        self.res = ''
        self.heros = ''
    def getEmailRwardProtobuffData(self,tmp):
        tmp.emailID = self.emailNID
        if self.items != '':
            itemstr = self.items.split('|')
            for ii in itemstr:
                tmpitem = tmp.pItems.add()
                itemdat = ii.split(';')
                itemx = _Item(itemCID = itemdat[1],itemLevel = 1,itemStar = 1,itemNumber = itemdat[2])
                itemx.getItemDataForProtobuff(tmpitem)
        elif self.heros != '':
            heros = self.heros.split('|')
            for hi in heros:
                tmphero = tmp.pHeros.add()
                herodat = hi.split(';')
                herox = _Hero(heroCID = herodat[1],heroLevel = 1,heroStar = 1)
                herox.getHeroDataForProtoBuff(tmphero)
        elif self.res != '':
            restmp = self.res.split('|')
            for ri in restmp:
                restmp2 = ri.split(';')
                if restmp2[0] == 1:     #1    元宝    acer
                    tmp.res.acer = restmp2[2]
                elif restmp2[0] == 2:   # 2    铜钱    copper
                    tmp.res.copper = restmp2[2]
                elif restmp2[0] == 3:   # 3    古籍    book
                    tmp.res.book = restmp2[2]
                elif restmp2[0] == 4:   # 4    将魂    soul
                    tmp.res.soul = restmp2[2]
                elif restmp2[0] == 5:   # 5    粮草    forage
                    tmp.res.forage = restmp2[2]
                elif restmp2[0] == 6:   # 6    军功    feats
                    tmp.res.feats = restmp2[2]
                elif restmp2[0] == 7:   # 7    勋章    medal
                    tmp.res.medal = restmp2[2]
                elif restmp2[0] == 8:   # 8    友情点  friendpoint
                    tmp.res.friendpoint = restmp2[2]
    def getEmailProtobuffData(self,tmp):
        tmp.pTitle = self.emailTitle
        tmp.pText = self.emailContent
        tmp.emailID = self.emailNID
        tmp.emailType = self.emailType
        tmp.lookType = self.emailLookType
        if self.items != '':
            itemstr = self.items.split('|')
            for ii in itemstr:
                tmpitem = tmp.pItems.add()
                itemdat = ii.split(';')
                itemx = _Item(itemCID = itemdat[1],itemLevel = 1,itemStar = 1,itemNumber = itemdat[2])
                itemx.getItemDataForProtobuff(tmpitem)
        elif self.heros != '':
            heros = self.heros.split('|')
            for hi in heros:
                tmphero = tmp.pHero.add()
                herodat = hi.split(';')
                herox = _Hero(heroCID = herodat[1],heroLevel = 1,heroStar = 1)
                herox.getHeroDataForProtoBuff(tmphero)
        elif self.res != '':
            restmp = self.res.split('|')
            for ri in restmp:
                restmp2 = ri.split(';')
                if restmp2[0] == 1:     #1    元宝    acer
                    tmp.res.acer = restmp2[2]
                elif restmp2[0] == 2:   # 2    铜钱    copper
                    tmp.res.copper = restmp2[2]
                elif restmp2[0] == 3:   # 3    古籍    book
                    tmp.res.book = restmp2[2]
                elif restmp2[0] == 4:   # 4    将魂    soul
                    tmp.res.soul = restmp2[2]
                elif restmp2[0] == 5:   # 5    粮草    forage
                    tmp.res.forage = restmp2[2]
                elif restmp2[0] == 6:   # 6    军功    feats
                    tmp.res.feats = restmp2[2]
                elif restmp2[0] == 7:   # 7    勋章    medal
                    tmp.res.medal = restmp2[2]
                elif restmp2[0] == 8:   # 8    友情点  friendpoint
                    tmp.res.friendpoint = restmp2[2]
    #设置邮件奖励
    def setReward(self,emailfrom,emailTo,rewardRes = '',rewardItems = '',rewardHeros = ''):#邮件奖励资源类型
        self.eFrom = emailfrom
        self.eTo = emailTo
        if rewardRes != '':
            self.res = rewardRes
        elif rewardItems != '':
            self.items = rewardItems
        elif rewardHeros != '':
            self.heros = rewardHeros
    #{'to':'toaccount','title','title','content','type',1,'items':'','heros':'','res':''}
    def getDictData(self):
        if self.eFrom == '' or self.eTo == '':
            return None
        else:
            tmpdic = {}
            tmpdic['to'] = self.eTo
            tmpdic['title'] = self.emailTitle
            tmpdic['content'] = self.emailContent
            tmpdic['type'] = self.emailType
            if not self.items == '':
                tmpdic['items'] = self.items
            elif not self.heros == '':
                tmpdic['heros'] = self.heros
            elif not self.res == '':
                tmpdic['res'] = self.res
            return tmpdic
            
class Email():
    def __init__(self,puser):
        self.user = puser
        self.eIndex = 1                 #创建邮件的临时ID
        self.userEmails = {}            #玩家所有已从数据库中取得,并已初始化了的邮件{emailNid,EmailData}
        self.newEmailIDs = []           #新接收的还没有从数据库中取出的邮件网络ID，
        self.newNoSendEmailIDs = []     #新接收的已从数据库中取出，但还未发送给玩家的邮件网络ID
        self.sendEmailData = {}         #新创建的存入邮件数据库的发送的邮件{indexID:emailData},数据存入数据库后，将数据库网络ID存入将要发送的perSendEmailData数组中
        self.perSendEmailData = []      #新创建的已存入数据库但还未发送出去的邮件网络ID,[emailNetID]
    def getEmailData(self,emailnid):
        if self.userEmails.has_key(emailnid):
            self.userEmails[emailnid].emailLookType = EmailFlog.flog_elTypeLookedAndRecive
            return self.userEmails[emailnid]
    def getNewEmailIDs(self):
        if self.newEmailIDs:
            return self.newEmailIDs
        elif self.newNoSendEmailIDs:
            return self.newNoSendEmailIDs
    def getNewEmailIDsFromSQLDB(self):      #新邮件内容已从数据库中取出
        self.newNoSendEmailIDs = self.newEmailIDs
        self.newEmailIDs = []
    def deleteEmailWithNID(self,enid):
        if self.userEmails.has_key(enid):
            self.userEmails.pop(enid)
    #创建发送数据
    def setSendEmailData(self,emailData):
        self.eIndex += 1
        if self.eIndex > 500:
            self.eIndex = 1
        emailData.emailNID = self.eIndex
        self.sendEmailData[self.eIndex] = emailData
        return self.eIndex
    #获取保存到数据库时的Email数据
    def getMailDicDataForCreateSql(self):
        #返回:emailtimeid,EmailData
        return self.sendEmailData.popitem()
    #获取所有玩家邮件
    def getProtobufDataForHeaveEmails(self,tmps):
        for ek in self.userEmails.keys():
            emailtmp = tmps.add()
            self.userEmails[ek].getEmailProtobuffData(emailtmp)
    def addNewEmailFromOtherWithType(self,emailNid,eType = 1):#邮件数据，邮件类型,1.数据来自数据库(竟技场,帮会,风云)，2.数据来自更新配置文件(更新补尝)
        if eType == 1:#通过初始化邮件类型初始分邮件
            self.newEmailIDs.append(emailNid)
        elif eType == 2:#能过配置文件初始化邮件，比如服务器更新补尝
            self.newEmailIDs.append(emailNid)
    