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
<<<<<<< HEAD
from models.app import postAppSchema,deleteAppSchema,getListAppSchema,getAppSchema,putAppSchema
=======
from models.app import postAppSchema,deleteAppSchema,getListAppSchema,getListAppsSchema,getAppSchema,putAppSchema
from webcreator.log import logger
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
from webcreator.response import ResponseCode, response_result

class AppResourceList(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
<<<<<<< HEAD
            print("========>", json_payload)
            data = getListAppSchema.load(json_payload)
            result = signalManager.actionGetApp.emit(**data)
            json_dumps = getListAppSchema.dump(result)
            return jsonify(json_dumps), 200
=======
            logger.warn(json_payload)
            data = getListAppSchema.load(json_payload)
            result = signalManager.actionGetListApp.emit(data)
            json_dumps = getListAppSchema.dump(result)
            if result[0]:
                json_dumps = getListAppsSchema.dump(result[1])
                logger.warn(json_dumps)
                return response_result(ResponseCode.OK, data=json_dumps, count=result[2])
            return response_result(ResponseCode.REQUEST_ERROR)
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.DB_ERROR)

<<<<<<< HEAD
    @jwt_required
=======
    @jwt_required(locations=["headers"])
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
    def post(self):
        try:
            json_payload = request.json
            data = postAppSchema.load(json_payload)
<<<<<<< HEAD
            result = signalManager.actionPostApp.emit(**data)
            json_dumps = postAppSchema.dump(result)
            return jsonify(json_dumps), 200
=======
            result = signalManager.actionPostApp.emit(data)
            if result[0] == False:
                # json_dumps = postAppSchema.dump(result)
                return response_result(ResponseCode.REQUEST_ERROR, msg=result[1])
            logger.warn(result)
            return response_result(ResponseCode.OK)
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.DB_ERROR)


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

<<<<<<< HEAD
    @jwt_required
=======
    @jwt_required(locations=["headers"])
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
    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 = getAppSchema.load(json_payload)
<<<<<<< HEAD
            result = signalManager.actionGetApp.emit(**data)
            json_dumps = getAppSchema.dump(result)
            return jsonify(json_dumps), 200
=======
            result = signalManager.actionGetApp.emit(uuid, data)
            if result[0]:
                json_dumps = getAppSchema.dump(result[1])
                return response_result(ResponseCode.OK, data=json_dumps)
            return response_result(ResponseCode.NO_DATA)
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.DB_ERROR)


<<<<<<< HEAD
    @jwt_required
=======
    @jwt_required(locations=["headers"])
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
    def put(self, uuid):
        try:
            json_payload = request.json
            print("========>", uuid, json_payload)
            data = putAppSchema.load(json_payload)
<<<<<<< HEAD
            result = signalManager.actionPutApp.emit(**data)
            json_dumps = putAppSchema.dump(result)
            return jsonify(json_dumps), 200
=======
            result = signalManager.actionPutApp.emit(uuid, data)
            if result[0] == True:
                # json_dumps = putAppSchema.dump(result)
                return response_result(ResponseCode.OK)
            return response_result(ResponseCode.NOTHING_CHANGE, msg=result[1])
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.DB_ERROR)


<<<<<<< HEAD
    @jwt_required
=======
    @jwt_required(locations=["headers"])
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
    def delete(self, uuid):
        try:
            json_payload = request.json
            print("========>", uuid, json_payload)
<<<<<<< HEAD
            data = deleteAppSchema.load(json_payload)
            result = signalManager.actionDeleteApp.emit(**data)
            json_dumps = deleteAppSchema.dump(result)
            return jsonify(json_dumps), 200
=======
            # data = deleteAppSchema.load(json_payload)
            result = signalManager.actionDeleteApp.emit(uuid)
            if result[0] == True:
                return response_result(ResponseCode.OK)
            return response_result(ResponseCode.REQUEST_ERROR, msg=result[1])
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.DB_ERROR)
