# /usr/bin/env python
# -*- coding: UTF-8 -*-
from __future__ import division
from numpy import floor, diff, array, mean, std


def load_in_flag(data, length, ctnum):
    if length <= 6000:
        sep = 80
        points = int(floor(length / 80))
    elif length <= 12800:
        sep = 160
        points = int(floor(length / 160))
    elif length <= 20000:
        sep = 250
        points = int(floor(length / 250))
    else:
        sep = 330
        points = int(floor(length / 330))

    if ctnum == 3:

        vote_ctnum_count_flag_max = 0
        vote_ctnum_count_flag_min = 0
        current_sum_fore = 0
        for i in ['ua', 'ub', 'uc']:
            vote_change_flag_max = 0
            vote_change_flag_min = 0
            bigger_count_max = 0
            bigger_count_min = 0
            current = data[i]
            maxValues = [max(current[sep * j:sep * (j + 1)]) for j in range(points)]
            minValues = [abs(min(current[sep * j:sep * (j + 1)])) for j in range(points)]
            for each in range(len(maxValues) - 1):
                if maxValues[each] - maxValues[0] < -0.1 * max(maxValues[0], maxValues[-1]):
                    vote_change_flag_max = each
                    break
            for each in range(vote_change_flag_max, len(maxValues)):
                if abs(maxValues[each] - maxValues[vote_change_flag_max - 1]) > 0.1 * max(maxValues[0],
                                                                                          maxValues[-1]):
                    bigger_count_max = bigger_count_max + 1

            if bigger_count_max == len(maxValues) - vote_change_flag_max:
                vote_ctnum_count_flag_max = vote_ctnum_count_flag_max + 1

            for each in range(len(minValues) - 1):
                if abs(minValues[each] - minValues[0]) > 0.1 * max(minValues[0], minValues[-1]):
                    vote_change_flag_min = each
                    break
            for each in range(vote_change_flag_min, len(minValues)):
                if abs(minValues[each] - minValues[vote_change_flag_min - 1]) > 0.1 * max(minValues[0],
                                                                                          minValues[-1]):
                    bigger_count_min = bigger_count_min + 1

            if bigger_count_min == len(minValues) - vote_change_flag_min:
                vote_ctnum_count_flag_min = vote_ctnum_count_flag_min + 1

        ctnum_count_flag_max = 0
        ctnum_count_flag_min = 0
        for i in ['ia', 'ib', 'ic']:
            current_change_flag_max = 0
            current_change_flag_min = 0
            bigger_count_max = 0
            bigger_count_min = 0
            current = data[i]
            maxValues = [max(current[sep * j:sep * (j + 1)]) for j in range(points)]
            minValues = [abs(min(current[sep * j:sep * (j + 1)])) for j in range(points)]
            for each in range(len(maxValues) - 1):
                if maxValues[each] - maxValues[0] > 0.3 * max(maxValues[0], maxValues[-1]):
                    current_change_flag_max = each
                    break
            for each in range(current_change_flag_max, len(maxValues)):
                if maxValues[each] - maxValues[current_change_flag_max - 1] > 0 or abs(
                                maxValues[each] - maxValues[current_change_flag_max - 1]) < 0.2 * max(maxValues[0],
                                                                                                      maxValues[-1]):
                    bigger_count_max = bigger_count_max + 1

            if bigger_count_max == len(maxValues) - current_change_flag_max:
                ctnum_count_flag_max = ctnum_count_flag_max + 1
            if sum(maxValues[0:current_change_flag_max]) < max(maxValues) and sum(
                    maxValues[0:current_change_flag_max]) > 0:
                current_sum_fore += 1

            for each in range(len(minValues) - 1):
                if minValues[each] - minValues[0] > 0.3 * max(minValues[0], minValues[-1]):
                    current_change_flag_min = each
                    break
            for each in range(current_change_flag_min, len(minValues)):
                if minValues[each] - minValues[current_change_flag_min - 1] > 0 or abs(
                                minValues[each] - minValues[current_change_flag_min - 1]) < 0.2 * max(minValues[0],
                                                                                                      minValues[-1]):
                    bigger_count_min = bigger_count_min + 1

            if bigger_count_min == len(minValues) - current_change_flag_min:
                ctnum_count_flag_min = ctnum_count_flag_min + 1
            if sum(minValues[0:current_change_flag_min]) < max(minValues) and sum(
                    minValues[0:current_change_flag_min]) > 0:
                current_sum_fore += 1

    elif ctnum == 2:

        vote_ctnum_count_flag_max = 0
        vote_ctnum_count_flag_min = 0
        current_sum_fore = 0
        for i in ['uab', 'ubc']:
            vote_change_flag_max = 0
            vote_change_flag_min = 0
            bigger_count_max = 0
            bigger_count_min = 0
            current = data[i]
            maxValues = [max(current[sep * j:sep * (j + 1)]) for j in range(points)]
            minValues = [abs(min(current[sep * j:sep * (j + 1)])) for j in range(points)]
            for each in range(len(maxValues) - 1):
                if maxValues[each] - maxValues[0] < -0.1 * max(maxValues[0], maxValues[-1]):
                    vote_change_flag_max = each
                    break
            for each in range(vote_change_flag_max, len(maxValues)):
                if abs(maxValues[each] - maxValues[vote_change_flag_max - 1]) > 0.1 * max(maxValues[0],
                                                                                          maxValues[-1]):
                    bigger_count_max = bigger_count_max + 1

            if bigger_count_max == len(maxValues) - vote_change_flag_max:
                vote_ctnum_count_flag_max = vote_ctnum_count_flag_max + 1

            for each in range(len(minValues) - 1):
                if abs(minValues[each] - minValues[0]) > 0.1 * max(minValues[0], minValues[-1]):
                    vote_change_flag_min = each
                    break
            for each in range(vote_change_flag_min, len(minValues)):
                if abs(minValues[each] - minValues[vote_change_flag_min - 1]) > 0.1 * max(minValues[0],
                                                                                          minValues[-1]):
                    bigger_count_min = bigger_count_min + 1

            if bigger_count_min == len(minValues) - vote_change_flag_min:
                vote_ctnum_count_flag_min = vote_ctnum_count_flag_min + 1

        ctnum_count_flag_max = 0
        ctnum_count_flag_min = 0
        for i in ['iab', 'ibc']:
            current_change_flag_max = 0
            current_change_flag_min = 0
            bigger_count_max = 0
            bigger_count_min = 0
            current = data[i]
            maxValues = [max(current[sep * j:sep * (j + 1)]) for j in range(points)]
            minValues = [abs(min(current[sep * j:sep * (j + 1)])) for j in range(points)]
            for each in range(len(maxValues) - 1):
                if maxValues[each] - maxValues[0] > 0.3 * max(maxValues[0], maxValues[-1]):
                    current_change_flag_max = each
                    break
            for each in range(current_change_flag_max, len(maxValues)):
                if maxValues[each] - maxValues[current_change_flag_max - 1] > 0 or abs(
                                maxValues[each] - maxValues[current_change_flag_max - 1]) < 0.2 * max(maxValues[0],
                                                                                                      maxValues[-1]):
                    bigger_count_max = bigger_count_max + 1

            if bigger_count_max == len(maxValues) - current_change_flag_max:
                ctnum_count_flag_max = ctnum_count_flag_max + 1
            if sum(maxValues[0:current_change_flag_max]) < max(maxValues) and sum(
                    maxValues[0:current_change_flag_max]) > 0:
                current_sum_fore += 1

            for each in range(len(minValues) - 1):
                if minValues[each] - minValues[0] > 0.3 * max(minValues[0], minValues[-1]):
                    current_change_flag_min = each
                    break
            for each in range(current_change_flag_min, len(minValues)):
                if minValues[each] - minValues[current_change_flag_min - 1] > 0 or abs(
                                minValues[each] - minValues[current_change_flag_min - 1]) < 0.2 * max(minValues[0],
                                                                                                      minValues[-1]):
                    bigger_count_min = bigger_count_min + 1

            if bigger_count_min == len(minValues) - current_change_flag_min:
                ctnum_count_flag_min = ctnum_count_flag_min + 1

            if sum(minValues[0:current_change_flag_min]) < max(minValues) and sum(
                    minValues[0:current_change_flag_min]) > 0:
                current_sum_fore += 1

    return ctnum_count_flag_max, ctnum_count_flag_min, vote_ctnum_count_flag_max, vote_ctnum_count_flag_min, current_sum_fore
