import requests


class UUAccount:
    def __init__(self, token):
        """
        :param token: 通过抓包获得的token
        """
        self.session = requests.Session()
        self.session.headers.update({'authorization': 'Bearer ' + token,
                                     'content-type': 'application/json; charset=utf-8',
                                     'user-agent': 'okhttp/3.14.9',
                                     'app-version': '5.0.5',
                                     'apptype': '4'})

    @staticmethod
    def __random_str(length):
        import random
        import string
        return ''.join(random.sample(string.ascii_letters + string.digits, length))

    @staticmethod
    def get_token_automatically():
        """
        引导用户输入手机号，发送验证码，输入验证码，自动登录，并且返回token
        :return: token
        """
        phone_number = input('输入手机号：')
        session_id = UUAccount.get_random_session_id()
        print('随机生成的session_id：', session_id)
        print('发送验证码结果：', UUAccount.send_login_sms_code(phone_number, session_id)['Msg'])
        sms_code = input('输入验证码：')
        response = UUAccount.sms_sign_in(phone_number, sms_code, session_id)
        print('登录结果：', response['Msg'])
        got_token = response['Data']['Token']
        print('token：', got_token)
        return got_token

    @staticmethod
    def get_random_session_id():
        return UUAccount.__random_str(32)

    @staticmethod
    def send_login_sms_code(phone, session: str):
        """
        发送登录短信验证码
        :param phone: 手机号
        :param session: 可以通过UUAccount.get_random_session_id()获得
        :return:
        """
        return requests.post('https://api.youpin898.com/api/user/Auth/SendSignInSmsCode',
                             json={'Mobile': phone,
                                   'Sessionid': session}).json()

    @staticmethod
    def sms_sign_in(phone, code, session):
        """
        通过短信验证码登录，返回值内包含Token
        :param phone: 发送验证码时的手机号
        :param code: 短信验证码
        :param session: 可以通过UUAccount.get_random_session_id()获得，必须和发送验证码时的session一致
        :return:
        """
        return requests.post('https://api.youpin898.com/api/user/Auth/SmsSignIn',
                             json={'Code': code,
                                   'Sessionid': session,
                                   'Mobile': phone}).json()

    def get_user_nickname(self):
        return self.call_api('GET', '/api/user/Account/getUserInfo').json()['Data']['NickName']

    def call_api(self, method, path, data=None):
        """
        调用API
        :param method: GET, POST, PUT, DELETE
        :param path: 请求路径
        :param data: 发送的数据
        :return:
        """
        url = 'https://api.youpin898.com' + path
        if method == 'GET':
            return self.session.get(url, params=data)
        elif method == 'POST':
            return self.session.post(url, json=data)
        elif method == 'PUT':
            return self.session.put(url, data=data)
        elif method == 'DELETE':
            return self.session.delete(url)
        else:
            raise Exception('Method not supported')

    def get_wait_deliver_list(self, game_id=730, return_offer_id=True):
        """
        获取待发货列表（出售）
        :param return_offer_id: 默认为True，是否返回steam交易报价号
        :param game_id: 游戏ID，默认为730(CSGO)
        :return: 待发货列表，格式为[{'order_id': '订单号', 'item_name': '物品名称', 'offer_id': 'steam交易报价号'}... , ...]
        """
        # data = self.call_api('POST', '/api/youpin/bff/trade/sell/page/v1/waitDeliver/waitDeliverList', data={
        #     "gameId": game_id,
        #     "pageIndex": 1,
        #     "pageSize": 1000
        # })
        # wait_deliver_list = data.json()['data']['waitDeliverList']
        # data_to_return = []
        # if wait_deliver_list is not None:
        #     for item in wait_deliver_list:
        #         if item['orderInfoVO']['orderType'] == 1:
        #             dict_to_append = dict()
        #             dict_to_append['order_id'] = item['orderInfoVO']['orderNo']
        #             dict_to_append['item_name'] = item['commodityInfoVO']['commodityName']
        #             if return_offer_id:
        #                 dict_to_append['offer_id'] = self.get_steam_offer_id_by_order_id(dict_to_append['order_id'])
        #             data_to_return.append(dict_to_append)
        data = self.call_api('POST', '/api/youpin/bff/trade/sale/v1/sell/list', data={
            "keys": "",
            "orderStatus": "140",
            "pageIndex": 1,
            "pageSize": 100
        }).json()['data']
        data_to_return = []
        for order in data['orderList']:
            data_to_return.append({
                'order_id': order['tradeOfferId'],
                'item_name': order['productDetail']['commodityName'],
            })
        return data_to_return
