# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : report.py
# @Time       : 2020/11/18 下午3:19
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------
import datetime
import json
import time
import uuid

import oss2
from flask_restful import Resource, reqparse
from flask import request, render_template, g
from flask import make_response

from app.api.engine import config
from app.service.report_service import get_report_list, delete_report, get_one_report, edit_report, make_report, edit_report_name
from app.service.template_manage_service import get_make_report_data
from app.utils.auth import login_require
from app.utils.format_transfer import npEncoder


class ReportHandlers(Resource):
    """."""

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

    @login_require
    def get(self):
        """获取新建报告数据."""
        self.parser.add_argument('id', type=str, required=True, help='模版id不能为空')
        self.parser.add_argument('customer_id', type=str, required=True, help='customer_id不能为空')
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        data = get_make_report_data(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('type', type=int, required=True, help='1为持仓报告2为诊断报告3周期报告')
        self.parser.add_argument('name', type=str, required=False, help='报告名称')
        self.parser.add_argument('customer_id', type=str, required=True, help='customer_id不能为空')
        self.parser.add_argument('report_data', type=str, required=True, help='report_data不能为空')
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        data = make_report(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return resp

    @login_require
    def put(self):
        """编辑报告名称."""
        self.parser.add_argument('type', type=int, required=True, help='1为持仓报告2为诊断报告3周期报告')
        self.parser.add_argument('id', type=int, required=True, help='报告id不能为空')
        self.parser.add_argument('name', type=str, required=True, help='报告名称不能为空')
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        data = edit_report_name(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return resp

    @login_require
    def delete(self, id):
        """."""
        pass


class ReportList(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为诊断报告3周期报告')
        self.parser.add_argument('name', type=str, required=False, help='报告名称或者客户姓名')
        self.parser.add_argument('pageNumber', type=int, required=True)
        self.parser.add_argument('pageSize', type=int, required=True)
        self.parser.add_argument('start_time', type=str, required=False)
        self.parser.add_argument('end_time', type=str, required=False)
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        data = get_report_list(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return resp

    @login_require
    def post(self):
        """保存报告."""
        pass

    @login_require
    def put(self):
        """编辑报告."""
        self.parser.add_argument('type', type=int, required=True, help='1为持仓报告2为诊断报告3周期报告')
        self.parser.add_argument('id', type=int, required=True, help='报告id不能为空')
        self.parser.add_argument('report_data', type=str, required=True, help='报告数据不能为空')
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        data = edit_report(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return resp

    @login_require
    def delete(self):
        """."""
        self.parser.add_argument('type', type=int, required=True, help='0为全部1为持仓报告2为诊断报告3周期报告')
        self.parser.add_argument('id', type=int, required=True, help='报告id不能为空')
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        delete_report(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": []
        }
        return resp


class FindReport(Resource):
    """."""

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

    @login_require
    def get(self):
        """获取单个报告."""
        self.parser.add_argument('type', type=int, required=True, help='1为持仓报告2为诊断报告3周期报告')
        self.parser.add_argument('id', type=str, required=False, help='')
        args = self.parser.parse_args()
        args['ifa_id'] = g.ifa_id
        data = get_one_report(args)
        resp = {
            "statusCode": "0000",
            "message": "成功",
            "attributes": data
        }
        return resp