# -*- coding: utf-8 -*-
__author__ = "Lee.le"
from Views.tools import *
from django.shortcuts import render, HttpResponse,HttpResponseRedirect, reverse
import shutil
import copy

report = HtmlName()

def status(request):
    """
    展示测试报告
    :param request:
    :return:
    """
    if request.method == 'POST':
        global reportname
        reportname = request.POST.get("reportname")
        delname = request.POST.get("delname")
        if delname != None:
            path = os.path.join(os.getcwd(), f"static/Report")
            file = path + f'/{delname}'
            if (os.path.exists(file)):
                shutil.rmtree(file)
        if reportname == None:
            reporthtml = ''
            report.set_report(reporthtml,reportname)
        else:
            reporthtml = f'.././static/Report/{reportname}/{reportname}.html'
            report.set_report(reporthtml, reportname)
            # return HttpResponseRedirect('/info/')
    else:
        reporthtml = ''
    reportlist, htmlname = getreport()
    htmlstate = copy.deepcopy(reportlist) # copy一个新的字典
    for i in htmlname:
        file_Path = os.path.join(os.getcwd(), f"static/Report/{i}/{i}.html")  #判断是否生成实测报告，并且备注状态
        file_Date = os.path.join(os.getcwd(), f"static/Report/{i}")
        filetime = os.stat(file_Date).st_ctime  # 文件创建时间
        nowtime = time.time()  # 当前时间
        overtime = (nowtime - filetime)
        if os.path.exists(file_Path):
            key = i.split("_")[1]
            htmlstate[key] = 'done'
        else:
            if overtime > 9000:
                key = i.split("_")[1]
                htmlstate[key] = 'false'
            else:
                key = i.split("_")[1]
                htmlstate[key] = 'doing'
    if get_config("progress") == 0:
        progressstatus = 0
    else:
        progressstatus = int(get_config("progress")/get_config("testcase")*100)

    return render(request, "report.html", {'report': reportlist, 'htmlstate': htmlstate, 'addr': get_addr(), 'progressstatus': progressstatus})


def reportinfo(request):
    reporthtml, reportname = report.get_report()
    return render(request, "info.html", {'reporthtml': reporthtml, 'addr': get_addr(), 'reportname': '当前报告：' + reportname})



def get_progress(request):
    reportlist, htmlname = getreport()  # 获取所有报告
    temp = 0  # 定义一个参数用来指定返回html的数据类型，1需要执行局部加载，0不需要执行局部加载
    for i in htmlname:  # 判断是否有报告未生成的，temp=1执行局部加载，并且把进度传到前端
        file_Path = os.path.join(os.getcwd(), f"static/Report/{i}/{i}.html")  # 判断是否有卫生而成报告的路径
        if not os.path.exists(file_Path):
            if get_config("progress") == 0:
                progressstatus = 0
                temp = 1
                break
            else:
                progressstatus = int(get_config("progress")/get_config("testcase")*100)
                temp = 1
                break
    if temp ==1:  # 当有报告未生成的时候，temp=1执行局部加载，并且把进度传到前端
        return HttpResponse(progressstatus)
    else:  # 条件不满足，返回404告诉前端不需要执行局部加载
        progressstatus = 404
        return HttpResponse(progressstatus)