'''
Author: your name
Date: 2021-07-15 09:33:39
LastEditTime: 2021-07-16 19:27:36
LastEditors: your name
Description: In User Settings Edit
FilePath: \evm-store\tools\build_out\views\package.20210715165358.py
'''
from flask import current_app, jsonify, request
from flask_restful import Resource
from flask_restful.reqparse import RequestParser
from flask_jwt_extended import ( jwt_required, get_jwt_identity )
from application.signal_manager import signalManager
from models.package import  getListPackageSchema, getListPackagesSchema, getPackageSchema
from webcreator.log import logger
from webcreator.response import ResponseCode, response_result

class PackageResourceList(Resource):
    def __init__(self):
        pass
        # 特殊参数，即不是从json获取参数的接口，可以将这个注释打开
        self.parser = RequestParser()

    def get(self):
        # 特殊参数，即不是从json获取参数的接口，可以将这个注释打开
        # self.parser.add_argument("page", type=int, location="args", default=1)
        # self.parser.add_argument("pageSize", type=int, location="args", default=15)
        # args = self.parser.parse_args()

        try:
            json_payload = request.json
            logger.warn(json_payload)
            data = getListPackageSchema.load(json_payload)
            result = signalManager.actionGetListPackage.emit(data)
            json_dumps = getListPackageSchema.dump(result)
            if result[0]:
                json_dumps = getListPackagesSchema.dump(result[1])
                logger.warn(json_dumps)
                return response_result(ResponseCode.OK, data=json_dumps, count=result[2])
            return response_result(ResponseCode.REQUEST_ERROR)
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.DB_ERROR)

class PackageResource(Resource):
    def __init__(self):
        pass
        # 特殊参数，即不是从json获取参数的接口，可以将这个注释打开
        # self.parser = RequestParser()

    @jwt_required(locations=["headers"])
    def get(self, uuid):
        # 特殊参数，即不是从json获取参数的接口，可以将这个注释打开
        # self.parser.add_argument("page", type=int, location="args", default=1)
        # self.parser.add_argument("pageSize", type=int, location="args", default=15)
        # args = self.parser.parse_args()

        try:
            json_payload = request.json
            print("========>", uuid, json_payload)
            data = getPackageSchema.load(json_payload)
            result = signalManager.actionGetPackage.emit(uuid, data)
            if result[0]:
                json_dumps = getPackageSchema.dump(result[1])
                return response_result(ResponseCode.OK, data=json_dumps)
            return response_result(ResponseCode.NO_DATA)
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.DB_ERROR)
