'''
Author: your name
Date: 2021-07-09 12:39:40
LastEditTime: 2021-07-15 12:06:33
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \evm-store\tools\build_out\views\file.py
'''
import traceback
from flask import current_app, jsonify, request, make_response, Response
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 webcreator.log import logger
from webcreator.response import ResponseCode, response_result

class FileInit(Resource):
    def __init__(self):
        # 特殊参数，即不是从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:
            result, message = signalManager.actionGetFileInit.emit()
            if result:
                return response_result(ResponseCode.HTTP_SUCCESS, data=result)
            return response_result(message)
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)


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

    @jwt_required(locations=["headers"])
    def get(self):
        # 特殊参数，即不是从json获取参数的接口，可以将这个注释打开
        self.parser.add_argument("disk", type=str, location="args", required=True)
        self.parser.add_argument("path", type=str, location="args", required=False, default=None)
        args = self.parser.parse_args()

        try:
            result, message = signalManager.actionGetFileContent.emit(args.disk, args.path)
            if result:
                logger.info(result)
                return response_result(ResponseCode.HTTP_SUCCESS, data=result)
            return response_result(message)
        except Exception as e:
            traceback.print_exc()
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)

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

    def get(self):
        # 特殊参数，即不是从json获取参数的接口，可以将这个注释打开
        self.parser.add_argument("disk", type=str, location="args", required=True)
        self.parser.add_argument("path", type=str, location="args", required=True)
        args = self.parser.parse_args()

        try:
            logger.info("============>")
            result, message = signalManager.actionGetFileDisk.emit(args.disk, args.path)
            if result:
                return response_result(ResponseCode.HTTP_SUCCESS, data=result)
            return response_result(message)
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)


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

    @jwt_required(locations=["headers"])
    def get(self):
        # 特殊参数，即不是从json获取参数的接口，可以将这个注释打开
        self.parser.add_argument("disk", type=str, location="args", required=True)
        self.parser.add_argument("path", type=str, location="args", required=False)
        args = self.parser.parse_args()

        try:
            result, message = signalManager.actionGetFileTree.emit(args.disk, args.path)
            if result:
                return response_result(ResponseCode.HTTP_SUCCESS, data=result)
            return response_result(message)
        except Exception as e:
            print(e)
            logger.error(e)
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)

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

    def get(self):
        # 特殊参数，即不是从json获取参数的接口，可以将这个注释打开
        self.parser.add_argument("disk", type=str, location="args", required=True)
        self.parser.add_argument("path", type=str, location="args", required=True)
        args = self.parser.parse_args()

        try:
            result, message = signalManager.actionGetFileDown.emit(args.disk, args.path)
            if result:
                resp, mime = result
                # return Response(resp, mimetype='text/xml')
                return Response(resp, content_type='{}; charset=utf-8'.format(mime))
            return response_result(message)
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)


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

    @jwt_required(locations=["headers"])
    def get(self):
        # 特殊参数，即不是从json获取参数的接口，可以将这个注释打开
        self.parser.add_argument("disk", type=str, location="args", required=True)
        self.parser.add_argument("path", type=str, location="args", required=True)
        args = self.parser.parse_args()

        try:
            result, message = signalManager.actionGetFilePreview.emit(args.disk, args.path)
            if result:
                content, mime = result
                resp = make_response(content)
                resp.mimetype = mime
                return resp
            return response_result(message)
        except Exception as e:
            traceback.print_exc()
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
