#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# module World
#
#    Copyright (C) 2019  AleaJactaEst
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

from tools import Enum
from tools import CSessionId
from tools import BitStream
from tools import CodeMsgXml
from tools import CStringManager

class World():
    def __init__(self):
        self.timestamp = 0
        self.ServerPeopleActive = 255
        self.ServerCareerActive = 255
        self.CharacterSummaries = []
        self.Mainlands = []
        self.CShardNames = []
        self.UserPrivileges = ''
        self.FreeTrial = False
        self.CurrentState = Enum.TState.st_unknown
        self.UseFemaleTitles = False
        self.MessageServerTemplate = {}
        self.StringManager = CStringManager.CStringManager()

    def CreaterCharacter(self, msgXml, HeadName):
        '''
            khanat-opennel-code/code/ryzom/client/src/connection.cpp # class CAHAskCreateChar : public IActionHandler
            khanat-opennel-code/code/ryzom/common/src/game_share/msg_client_server.h # void setupFromCharacterSummary (const CCharacterSummary &cs)
            khanat-opennel-code/code/ryzom/common/src/game_share/msg_client_server.h # void serialBitMemStream(NLMISC::CBitMemStream &f)
        '''
        Slot = 0
        SheetId = 0
        #Mainland= 302
        cSessionId = CSessionId.CSessionId(self.CShardNames[0].SessionId)

        # [A-Za-z]{3,15}  # quand je pense qu'ils ont code pour avoir du UTF !!!!!
        name = ''
        for car in HeadName:
            if car >= 'a' and car <= 'z':
                pass
            elif car >= 'A' and car <= 'Z':
                pass
            elif car >= '0' and car <= '9':
                car = chr(ord(car)+ord('A')-ord('0'))
            else:
                car ='Z'
            name += car
        #name = 'Tester'
        People  = 1
        Sex = 0
        StartPoint = 1
        NbPointFighter = 2
        NbPointCaster =  1
        NbPointCrafter = 1
        NbPointHarvester =  1
        GabaritHeight = 9
        GabaritTorsoWidth = 10
        GabaritArmsWidth  = 7
        GabaritLegsWidth  = 4
        GabaritBreastSize = 7
        MorphTarget1 = 3
        MorphTarget2 = 3
        MorphTarget3 = 3
        MorphTarget4 = 5
        MorphTarget5 = 5
        MorphTarget6 = 5
        MorphTarget7 = 4
        MorphTarget8 = 4
        EyesColor = 0
        Tattoo = 0
        HairType = 5
        HairColor     = 1
        JacketColor   = 0
        TrousersColor = 0
        HatColor      = 1
        ArmsColor     = 0
        HandsColor    = 0
        FeetColor     = 0
        msgout = BitStream.BitStream()
        # GenericMsgHeaderMngr.pushNameToStream("CONNECTION:CREATE_CHAR", out))
        ref = CodeMsgXml.CodeMsgXml(msgXml, 'CONNECTION:CREATE_CHAR')
        for size, value, id in ref:
            msgout.internalSerial(value, size, typeName=id)
        # khanat-opennel-code/code/ryzom/common/src/game_share/msg_client_server.h # void serialBitMemStream(NLMISC::CBitMemStream &f)
        msgout.pushUint8(Slot)
        msgout.pushUint32(SheetId)
        cSessionId.push(msgout)
        msgout.pushUString(name)
        msgout.pushUint8(People)
        msgout.pushUint8(Sex)

        msgout.pushUint8(NbPointFighter)
        msgout.pushUint8(NbPointCaster)
        msgout.pushUint8(NbPointCrafter)
        msgout.pushUint8(NbPointHarvester)

        msgout.pushSint32(StartPoint)

        msgout.pushSint8(HairType)
        msgout.pushSint8(HairColor)

        #  GabaritHeight => 0 - 15
        msgout.pushSint8(GabaritHeight)
        msgout.pushSint8(GabaritTorsoWidth)
        msgout.pushSint8(GabaritArmsWidth)
        msgout.pushSint8(GabaritLegsWidth)
        msgout.pushSint8(GabaritBreastSize)

        # MorphTarget1 =>  0 - 7
        msgout.pushSint8(MorphTarget1)
        msgout.pushSint8(MorphTarget2)
        msgout.pushSint8(MorphTarget3)
        msgout.pushSint8(MorphTarget4)
        msgout.pushSint8(MorphTarget5)
        msgout.pushSint8(MorphTarget6)
        msgout.pushSint8(MorphTarget7)
        msgout.pushSint8(MorphTarget8)
        # EyesColor => 0 - 7
        msgout.pushSint8(EyesColor)
        # Tattoo) => 0 = neutral, 1 - 64 Tattoo
        msgout.pushSint8(Tattoo)

        #// color for equipement slots (Only for pre-equipped perso created with sheet)
        msgout.pushSint8(JacketColor)
        msgout.pushSint8(TrousersColor)
        msgout.pushSint8(HatColor)
        msgout.pushSint8(ArmsColor)
        msgout.pushSint8(HandsColor)
        msgout.pushSint8(FeetColor)

        #self.Commands.append(msgout)
        return msgout


    def impulsionCreateChar(self, msgXml, uid):
        '''
            khanat-opennel-code/code/ryzom/server/src/frontend_service/uid_impulsions.cpp # static void impulsionCreateChar(uint32 uid, NLMISC::CBitMemStream &bms, NLMISC::TGameCycle gameCycle)
        '''
        _ = self.CreaterCharacter(msgXml)
        # bms = self.CreaterCharacter(msgXml)
        #msgout = CMessage("CREATE_CHAR")
        #msgout.serial(uid, bms)

    def SelectChar(self,  msgXml, PlayerSelectedSlot):
        '''
            khanat-opennel-code/code/ryzom/client/src/far_tp.cpp # void CFarTP::selectCharAndEnter()
        '''
        msgout = BitStream.BitStream()
        ref = CodeMsgXml.CodeMsgXml(msgXml, 'CONNECTION:SELECT_CHAR')
        for size, value, id in ref:
            msgout.internalSerial(value, size, typeName=id)
        msgout.pushUint8(PlayerSelectedSlot)
        return msgout