# /usr/bin/env python
# -*- coding: UTF-8 -*-

def three_short_inside(conv_data, ctnum):
    if ctnum == 3:
        flag_a = 0
        flag_b = 0
        flag_c = 0
        index_a = []
        index_b = []
        index_c = []
        length = len(conv_data['ic'])
        conv_data['ia'] = conv_data['ia'] + conv_data['ib'] + conv_data['ic']
        conv_data['ua'] = conv_data['ua'] + conv_data['ub'] + conv_data['uc']

        i = 0
        for each_pair_i, each_pair_u in zip(conv_data['ia'], conv_data['ua']):
            if i > 0:
                if each_pair_i - conv_data['ia'][0] > conv_data['ia'][0] * 0.07 and conv_data['ua'][0] - each_pair_u > \
                                conv_data['ua'][0] * 0.08:
                    if i < length:
                        index_a.append(i)
                        flag_a = 1
                    elif i >= length and i < 2 * length:
                        index_b.append(i)
                        flag_b = 1
                    else:
                        index_c.append(i)
                        flag_c = 1
            i += 1
        if len(index_c) > 0:
            index_c = [x - 2 * length for x in index_c]
        if len(index_b) > 0:
            index_c = [x - length for x in index_c]
        set_len_abc = len(set(set(index_a) & set(index_b) & set(index_c)))
        if flag_a == 1 and flag_b == 1 and flag_c == 1 and set_len_abc > 0:
            return 3, u'三相短路ABC', u'内部故障'
        else:
            return 0, 'E',u'内部无故障'


def three_short_outside(conv_data, ctnum):
    if ctnum == 3:
        flag_a = 0
        index_c = []
        flag_b = 0
        index_a = []
        flag_c = 0
        index_b = []
        length = len(conv_data['ic'])
        conv_data['ia'] = conv_data['ia'] + conv_data['ib'] + conv_data['ic']
        conv_data['ua'] = conv_data['ua'] + conv_data['ub'] + conv_data['uc']

        i = 0
        for each_pair_i, each_pair_u in zip(conv_data['ia'], conv_data['ua']):
            if i > 0:
                if conv_data['ia'][0] - each_pair_i > conv_data['ia'][0] * 0.07 and conv_data['ua'][0] - each_pair_u > \
                                conv_data['ua'][0] * 0.08:
                    if i < length:
                        flag_a = 1
                    elif i >= length and i < 2 * length:
                        flag_b = 1
                    else:
                        flag_c = 1
            i += 1
        if len(index_c) > 0:
            index_c = [x - 2 * length for x in index_c]
        if len(index_b) > 0:
            index_c = [x - length for x in index_c]
        set_len_abc = len(set(set(index_a) & set(index_b) & set(index_c)))
        if flag_a == 1 and flag_b == 1 and flag_c == 1 and set_len_abc > 0:
            return 3, u'三相短路ABC', u'外部故障'
        else:
            return 0, 'E', u'外部无故障'
