# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : template_manage.py
# @Time       : 2021/1/6 下午6:25
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------
import json

from flask_restful import Resource, reqparse
from flask import request, g

from app.api.engine import redis
from app.config.default_template_params import hold_default_template, hold_default_data, diagnose_default_data, \
    diagnose_default_template
from app.service.template_manage_service import add_template, update_template, delete_template, get_template_list, \
    get_default_template, set_default_template
from app.utils.auth import login_require
from app.utils.format_transfer import npEncoder
from flask import jsonify
from app.utils.jinjia2html_v2 import DataIntegrate
import numpy as np


class TemplateManage(Resource):
    """."""

    def __init__(self):
        """."""
        self.parser = reqparse.RequestParser()

    @login_require
    def get(self):
        """获取模版数据."""
        self.parser.add_argument('id', type=str, required=True, help='id不能为空')
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        data = get_default_template(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return json.dumps(resp, cls=npEncoder, ensure_ascii=False)

    @login_require
    def post(self):
        """新建定义模版."""
        self.parser.add_argument('name', type=str, required=True, help='模版名称不能为空')
        self.parser.add_argument('custom_template', type=str, required=True, help='模版数据不能为空')
        self.parser.add_argument('type', type=int, required=True, help='模版类型不能为空')
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        add_template(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": []
        }
        return resp

    @login_require
    def put(self):
        """更新模版."""
        self.parser.add_argument('id', type=str, required=True, help='模版id')
        self.parser.add_argument('template_data', type=str, required=False, help='模版数据')
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        update_template(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": []
        }
        return resp

    @login_require
    def delete(self):
        """删除模版."""
        self.parser.add_argument('id', type=str, required=True, help='模版id')
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        res = delete_template(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": []
        }
        if not res:
            resp = {
                "statusCode": "9002",
                "message": "删除失败",
                "attributes": []
            }
        return resp


class TemplateList(Resource):
    """."""

    def __init__(self):
        """."""
        self.parser = reqparse.RequestParser()

    @login_require
    def get(self):
        """获取模版列表分页数据."""
        self.parser.add_argument('type', type=int, required=True, help='0为全部1为持仓报告2为诊断报告')
        self.parser.add_argument('name', type=str, required=False, help='模版名称')
        self.parser.add_argument('pageNumber', type=int, required=False)
        self.parser.add_argument('pageSize', type=int, required=False)
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        data = get_template_list(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return resp

    @login_require
    def put(self):
        """设置默认模版"""
        self.parser.add_argument('id', type=str, required=True, help='模版id')
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        data = set_default_template(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return resp
