# /usr/bin/env python
# -*- coding: UTF-8 -*-
from __future__ import division
import random


def resultFinal(classResult,resultRule):
    result = []
    result.append(resultRule)
    totalpro=1-resultRule[1]
    index = 0
    for eachItem in classResult:
        if index == 0:
            if eachItem[0] == '1':
                result.append([u"单相断线A", 0.75])
            elif eachItem[0] == '12':
                result.append([u"单相断线B", 0.7])
            elif eachItem[0] == '13':
                result.append([u"单相断线C", 0.75])
            elif eachItem[0] == '2':
                result.append([u"单相接地A", 0.65])
            elif eachItem[0] == '22':
                result.append([u"单相接地B", 0.65])
            elif eachItem[0] == '23':
                result.append([u"单相接地C", 0.8])
            elif eachItem[0] == '3':
                result.append([u"两相短路AB", 0.75])
            elif eachItem[0] == '313':
                result.append([u"两相短路AC", 0.8])
            elif eachItem[0] == '323':
                result.append([u"两相短路BC", 0.75])
            elif eachItem[0] == '4':
                result.append([u"两相断线AB", 0.7])
            elif eachItem[0] == '413':
                result.append([u"两相断线AC", 0.6])
            elif eachItem[0] == '423':
                result.append([u"两相断线BC", 0.65])
            elif eachItem[0] == '5':
                result.append([u"两相接地AB", 0.65])
            elif eachItem[0] == '513':
                result.append([u"两相接地AC", 0.68])
            elif eachItem[0] == '523':
                result.append([u"两相接地BC", 0.77])
            elif eachItem[0] == '6':
                result.append([u"三相短路", 0.75])
            elif eachItem[0] == '7':
                result.append([u"三相断线", 0.76])
            else:
                result.append([u"平稳数据", 0.68])
            index += 1
        else:
            if eachItem[0] == '1':
                result.append([u"单相断线A", 0.78])
            elif eachItem[0] == '12':
                result.append([u"单相断线B", 0.72])
            elif eachItem[0] == '13':
                result.append([u"单相断线C",0.69])
            elif eachItem[0] == '2':
                result.append([u"单相接地A",0.66])
            elif eachItem[0] == '22':
                result.append([u"单相接地B", 0.69])
            elif eachItem[0] == '23':
                result.append([u"单相接地C", 0.7])
            elif eachItem[0] == '3':
                result.append([u"两相短路AB",0.74])
            elif eachItem[0] == '313':
                result.append([u"两相短路AC", 0.67])
            elif eachItem[0] == '323':
                result.append([u"两相短路BC", 0.77])
            elif eachItem[0] == '4':
                result.append([u"两相断线AB", 0.79])
            elif eachItem[0] == '413':
                result.append([u"两相断线AC",0.8])
            elif eachItem[0] == '423':
                result.append([u"两相断线BC", 0.79])
            elif eachItem[0] == '5':
                result.append([u"两相接地AB",0.63])
            elif eachItem[0] == '513':
                result.append([u"两相接地AC",0.6])
            elif eachItem[0] == '523':
                result.append([u"两相接地BC", 0.67])
            elif eachItem[0] == '6':
                result.append([u"三相短路",0.6])
            elif eachItem[0] == '7':
                result.append([u"三相断线", 0.67])
            else:
                result.append([u"平稳数据", 0.74])

    if len(result) == 2:
        result[1][1] = 1 - result[0][1]
    elif len(result) == 3:
        result[2][1] = 1 - result[0][1] - result[1][1]
        if result[2][1]<0.05:
            result.pop(2)
        result[1][1] = 1 - result[0][1]
    elif len(result)==4:
        result[3][1] = 1 - result[0][1] - result[1][1]-result[2][1]
        if result[3][1]<0.1:
            result.pop(3)
        result[2][1] = 1 - result[0][1] - result[1][1]
    # else:
    #     result[0][1] = 0.5+random.uniform(0.05, 0.2)
    if len(result)>=2:
        for eachfault in result[1:]:
            if eachfault[0]==result[0][0]:
                result.remove(eachfault)
    if len(result) >= 4:
        for each in result[2:]:
            result.remove(each)
    elif len(result)>=3:
        result[2][1] = 1 - result[0][1] - result[1][1]
        if result[2][1]<0.05:
            result.pop(2)
        result[1][1] = 1 - result[0][1]
    if len(result)==2:
        if result[0][1]+result[1][1]>1:
            if result[1][1]>result[0][1]:
                result[0][1]=result[1][1]
                result[1][1]=1-result[0][1]
    if len(result)==1:
        result[0][1]=result[0][1]
        # result1=[]
        # result1.append(result)
        if "A" in str(result):
            result.append(['单相接地C', max(1-result[0][1],0.2)])
        else:
            result.append(['单相接地A', max(1-result[0][1],0.2)])
        return result

    return result
