def processData():
    
    if os.path.isfile('DataSet/Train/processedData.xlsx'):
        workbook = openpyxl.load_workbook("DataSet/Train/processedData.xlsx", use_iterators=True)
        sheet = workbook.worksheets[0]

        games = []
        for row in sheet.iter_rows():
            tempArray = []
            for idx,k in enumerate(row):
                tempArray.append(k.internal_value)
                
            games.append(tempArray)
    else:
        games, home_win, away_win, draw = processingData.startProcessingData(start_season_begin_year, start_season_end_year, NUMBER_OF_SEASONS, "Train")
        book = xlsxwriter.Workbook('DataSet/Train/processedData.xlsx')
        sheet = book.add_worksheet("Sheet 1")
        
        for i in range(0,len(games)):
            sheet.write(i,0,games[i][0])
            sheet.write(i,1,games[i][1])
            sheet.write(i,2,games[i][2])
            sheet.write(i,3,games[i][3])
            sheet.write(i,4,games[i][4])
            sheet.write(i,5,games[i][5])
            sheet.write(i,6,games[i][6])
            sheet.write(i,7,games[i][7])
        book.close()
    
    return games    
            sheet.write(i,5,games[i][5])
            sheet.write(i,6,games[i][6])
            sheet.write(i,7,games[i][7])
        book.close()
    
    return games    
    
#writing data in Excel
if isSettingDataEnabled == True:
    for i in range(0,NUMBER_OF_SEASONS):
        seasonName = "DataSet/Train/season" + str(start_season_begin_year-i) + "-" + str(start_season_end_year-i) 
        list_of_teams,results = settingData.loadSeason(seasonName + ".html")
        settingData.writeSeasonInExcel(seasonName + ".xlsx",list_of_teams,results)

#traing ann
games, home_win, away_win, draw = processingData.startProcessingData(start_season_begin_year, start_season_end_year, NUMBER_OF_SEASONS, "Train")
#statistic_matrix = settingData.createStatisticMatrix(home_win, away_win, draw)
#settingData.printProcessedData(games, statistic_matrix)
#games = processData() #write games in excel file
print 'Processing train data is done!'
input_list, output_list = trainAnn.prepareDataForAnn(games)
output = trainAnn.convertOutput(output_list)
ann = trainAnn.create_ann(128,7,3)
ann = trainAnn.train_ann(ann, input_list, output)

#testing ann

'''for i in range(0,1):
        seasonName = "DataSet/Test/season" + str(2015-i) + "-" + str(2016-i) 
        list_of_teams,results = settingData.loadSeason(seasonName + ".html")
        settingData.writeSeasonInExcel(seasonName + ".xlsx",list_of_teams,results)'''
def predictResult(beginSeasonYear, endSeasonYear, roundNumber, ann):
    current_standing_coef, previous_standing1_coef, previous_standing2_coef, previous_standing3_coef, goal_difference, table, current_match_index = processingData.startProcessingData(2015, 2016, 1, "Predict")
    
    #processing_workbook = openpyxl.load_workbook("DataSet/Predict/season" + str(beginSeasonYear) + '-' + str(endSeasonYear) + "round" + str(roundNumber) + ".xlsx" , use_iterators=True)
    #processing_sheet = processing_workbook.worksheets[0]       
    
    path = "DataSet/Predict/season" + str(beginSeasonYear) + '-' + str(endSeasonYear) + "round" + str(roundNumber) + ".xlsx"
    workbook = openpyxl.load_workbook(path, use_iterators=True)
    sheet = workbook.worksheets[0]    
    
    input_list = []
    
    alphabet = ['1', 'X', '2']
    home_list = []
    away_list = []
      
    for idx,row in enumerate(sheet.iter_rows()):
        home_team_name = row[0].internal_value.strip().encode('utf-8')
        home_list.append(home_team_name)
        away_team_name = row[1].internal_value.strip().encode('utf-8')
        away_list.append(away_team_name)
        home_team_strength = sum(goal_difference[home_team_name]) + current_standing_coef[home_team_name] + previous_standing1_coef[home_team_name] + previous_standing2_coef[home_team_name] + previous_standing3_coef[home_team_name]
        away_team_strength = sum(goal_difference[away_team_name]) + current_standing_coef[away_team_name] + previous_standing1_coef[away_team_name] + previous_standing2_coef[away_team_name] + previous_standing3_coef[away_team_name]
        
        temp = [0] * 7
        
        temp[0] = int(round(home_team_strength - away_team_strength,2))
        temp[1] = round(home_team_strength)
        temp[2] = round(away_team_strength)
        
        if current_match_index[home_team_name]==0:
            index_of_previous_match_home = NUMBER_OF_PREVIOUS_GAMES-1
        else:
            index_of_previous_match_home = current_match_index[home_team_name]-1
        if current_match_index[away_team_name]==0:
            index_of_previous_match_away = NUMBER_OF_PREVIOUS_GAMES-1
        else:
            index_of_previous_match_away = current_match_index[away_team_name]-1
            
        if index_of_previous_match_home >= 1:
            temp[3] = sum(goal_difference[home_team_name][index_of_previous_match_home-1:index_of_previous_match_home+1])
        else:
            temp[3] = goal_difference[home_team_name][index_of_previous_match_home] + goal_difference[home_team_name][NUMBER_OF_PREVIOUS_GAMES-1]
                    
        if index_of_previous_match_away >= 1:
            temp[4] = sum(goal_difference[away_team_name][index_of_previous_match_away-1:index_of_previous_match_away+1])
        else:
            temp[4] = goal_difference[away_team_name][index_of_previous_match_away] + goal_difference[away_team_name][NUMBER_OF_PREVIOUS_GAMES-1]

        temp[5] = round(current_standing_coef[home_team_name], 2)
        temp[6] = round(current_standing_coef[away_team_name], 2)
     
        input_list.append(temp)
        
    
    #use ann----------------------------
    results_test = ann.predict(np.array(input_list, np.float32))
    
    book = xlsxwriter.Workbook(path)
    sh = book.add_worksheet("Sheet 1")
    
    for i in range(0,len(results_test)):
        #processing_workbook.get_sheet(0).write(i,2,alphabet[winner(results_test[i])])
        #processing_sheet.write(i,2,alphabet[winner(results_test[i])])
        sh.write(i,0,home_list[i])
        sh.write(i,1,away_list[i])
        sh.write(i,2,alphabet[winner(results_test[i])])
        
    book.close()