'''
Author: your name
Date: 2021-07-09 12:39:40
LastEditTime: 2021-07-17 19:56:04
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \evm-store\tools\build_out\views\file.py
'''
import traceback
from pathlib import Path
from flask import current_app, jsonify, request, make_response, Response
from flask_restful import Resource
from werkzeug.datastructures import FileStorage
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 { 'config': result, 'result': { 'message': None, 'status': "success" } }
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        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:
                response = { 'result': { 'message': None, 'status': "success" } }
                response.update(result)
                return response
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as e:
            traceback.print_exc()
            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:
                response = { 'result': { 'message': None, 'status': "success" }, 'directories': None }
                response.update({ 'directories': result })
                return response
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        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=False)
        args = self.parser.parse_args()

        try:
            result, message = signalManager.actionGetFileDisk.emit(args.disk)
            if result:
                return { 'information': list(message), 'result': { 'message': "Disk selected!", 'status': "success" } }
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)

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

    def post(self):
        self.parser.add_argument("disk", type=str, location="args", required=True)
        self.parser.add_argument("path", type=str, location="args", required=True)
        self.parser.add_argument("file", type=FileStorage, location="files", required=True)
        args = self.parser.parse_args()

        try:
            file = request.files.get("file") # args.get('file')
            result, message = signalManager.actionPostFileUpdate.emit(args.disk, args.path, file)
            if result:
                return { 'config': result, 'result': { 'message': None, 'status': "success" } }
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as e:
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)

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

    def get(self):
        self.parser.add_argument("disk", type=str, location="args", required=True)
        self.parser.add_argument("path", type=str, location="args", required=True)
        self.parser.add_argument("overwrite", type=int, location="args", default=0, required=True)
        self.parser.add_argument("files", type=FileStorage, location="files", required=True)
        args = self.parser.parse_args()

        try:
            fileList = request.files.getlist('fileList')
            result, message = signalManager.actionPostFileUpload.emit()
            if result:
                return { 'config': result, 'result': { 'message': None, 'status': "success" } }
            return { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as 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:
            logger.info(args.path)
            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 { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as e:
            traceback.print_exc()
            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 { 'information': list(message), 'result': { 'message': "no data", 'status': "fail" } }
        except Exception as e:
            traceback.print_exc()
            current_app.logger.error(e)
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
