import asyncio
import json
import random
from pb import enums, frame
from pb import lobby as hall
import websockets
import utils
from functools import wraps


class Lobby:
    def __init__(self, usr, addr):
        self._user = usr
        self._addr = addr
        self._conn = None

    async def connect(self):
        self._conn = await websockets.connect(self._addr)

    async def close(self):
        await self._conn.close()

    async def request(self, func):
        try:
            req = frame.Request()
            mid, name, bindata = func()
            if mid is None:
                return
            #print(name)
            req.msgId = mid
            req.guid = self._user.guid
            req.msgName = name
            req.req = bindata
            msg = req.encode_to_bytes()
            # send
            await self._conn.send(msg)
            # recv
            r = await self._conn.recv()
            # dispatch
            self.dispatch(r)
        except Exception as e:
            print('request exception:', e)

    def C_Login(self):
        p = hall.C_Login()
        p.token = self._user.tk.token
        pbin = p.encode_to_bytes()
        return 0, 'C_Login', pbin

    def C_Heartbeat(self):
        p = hall.C_Heartbeat()
        p.ts = 0
        return 5, 'C_Heartbeat', p.encode_to_bytes()

    def C_ChildrenItemsOf(self):
        p = hall.C_ChildrenItemsOf()
        p.parentId = random.randint(1, 7)
        return 11, 'C_ChildrenItemsOf', p.encode_to_bytes()

    def C_EnterGame(self):
        return None, '', b''

    def C_LeaveGame(self):
        return None, '', b''

    def C_UserSettings(self):
        p = hall.C_UserSettings()
        p.nickname = utils.gen_nickname()
        p.icon = random.randint(1, 9)
        p.qq = utils.gen_qq()
        p.wechat = utils.gen_wechat()
        return 30, 'C_UserSettings', p.encode_to_bytes()
    
    def C_UpdateBankBalance(self):
        p = hall.C_UpdateBankBalance()
        p.delta = 100
        return 32, 'C_UpdateBankBalance', p.encode_to_bytes()

    def C_UpdateBankPassword(self):
        return None, '', b''

    def C_UserBalance(self):
        p = hall.C_UserBalance()
        return 101, 'C_UserBalance', p.encode_to_bytes()
    
    def C_BindAlipay(self):
        p = hall.C_BindAlipay()
        p.alipay = utils.gen_nickname()
        p.name = utils.gen_nickname()
        return 44, 'C_BindAlipay', p.encode_to_bytes()
    
    def C_PrivateMail(self):
        return 50, 'C_PrivateMail', hall.C_PrivateMail().encode_to_bytes()

    def C_Activity(self):
        return 55, 'C_Activity', hall.C_Activity().encode_to_bytes()
    
    def C_Notice(self):
        return 57, 'C_Notice', hall.C_Notice().encode_to_bytes()
    
    def C_Marquee(self):
        return 54, 'C_Marquee', hall.C_Marquee().encode_to_bytes()

    def dispatch(self, response):
        res = frame.Response()
        try:
            res.parse_from_bytes(response)
            print('response:', res.msgName)
            if res.msgName == 'S_Heartbeat':
                pass
            elif res.msgName == '':
                pass
            elif res.msgName == '':
                pass
            else:
                pass

        except Exception as e:
            print('dispatch exception:', e)


if __name__ == "__main__":
    l = Lobby(1, 2)
    l.request(l.C_Login)
