# -*- coding: utf8 -*-

import datetime as dt
import sys
from time import sleep
from datetime import datetime,timedelta
from datetime import date
import os
from scrapUtil import get_pages, get_tables
from race import Race

def get_main_race_id():
	main_race_info = {}
	url = r"https://race.netkeiba.com/top/"
	sp = get_pages(url)
	elem =sp.find("a", class_="PickupRace_LinkBtn")
	pos = str(elem).rfind('race_id=')+8
	main_race_id=str(elem)[pos:pos+12]
	return main_race_id

def get_else_date_main_race_id(main_race_id):
	# 別日のレースを取得
	else_date_main_race_id_pre = ''
	url = r"https://race.netkeiba.com/race/shutuba.html?race_id="+main_race_id
	sp = get_pages(url)
	elem =sp.find("dl", id="RaceList_DateList")
	nd = elem.find("dd",class_="Active")
	pos = str(nd).rfind('kaisai_date=')+12
	now_id_kaisai_date = str(nd)[pos:pos+8]
	a = elem.findAll("a")
	for aa in a:
		pos = str(aa).rfind('kaisai_date=')+12
		y=date(int(now_id_kaisai_date[:4]),int(now_id_kaisai_date[4:6]),int(now_id_kaisai_date[6:])) - timedelta(days=1)
		t=date(int(str(aa)[pos:pos+8][:4]),int(str(aa)[pos:pos+8][4:6]),int(str(aa)[pos:pos+8][6:]))
		if int(str(aa)[pos:pos+8]) == int(now_id_kaisai_date) - 1 or int(str(aa)[pos:pos+8]) == int(now_id_kaisai_date) + 1 or y==t:
			pos2 = str(aa).rfind('kaisai_id=')+10
			else_date_main_race_id_pre = str(aa)[pos2:pos2+10]
	else_date_main_race_id = else_date_main_race_id_pre + '11'
	return else_date_main_race_id

def get_else_races_id(main_race_id):
#メインレース会場のその他レース
	else_races_id = []
	main_race_no = main_race_id[10:12]
	main_place_race_id = main_race_id[0:10]
	# for race_no in range(int(main_race_no)):
	for race_no in range(12):
		else_race_no = str(race_no+1).rjust(2,'0')
		if else_race_no == main_race_no:
	 		pass
		else:
			else_races_id.append(main_place_race_id + else_race_no)
	return else_races_id

def get_else_places_main_race_id(main_race_id):
# 他会場のメインレース
	url = r"https://race.netkeiba.com/race/shutuba.html?race_id="+main_race_id
	sp = get_pages(url)
	else_place_main_races_id = []
	elem =sp.find("div", class_="RaceKaisaiWrap")
	a = elem.findAll("a")
	for aa in a:
 		pos = str(aa).rfind('race_id=')+8
 		else_place_main_race_id = str(aa)[pos:pos+12]
 		if else_place_main_race_id != main_race_id:
  			else_place_main_races_id.append(else_place_main_race_id)
	return else_place_main_races_id

def get_entered_info(race_id):
	url = r"https://race.netkeiba.com/race/shutuba.html?race_id="
	data = get_tables(url+race_id)[0]
	dd = data.rename(columns={'馬体重(増減)':'馬体重'})
	header = ['枠', '馬番','馬名','性齢','斤量','騎手','厩舎','馬体重']
	return dd[header]

if __name__ == '__main__':
	main_race_id = get_main_race_id()
	race_ids = []
	choice = input('土曜日：１　日曜日：２')
	if choice == 2:
		main_race_id = get_else_date_main_race_id(main_race_id) # 土曜のみ
	else_places_main_race_ids = get_else_places_main_race_id(main_race_id)
	race_ids.append(main_race_id)
	race_ids += else_places_main_race_ids
	#共通　他会場メイン以外22レース
	all_race_ids = race_ids.copy()
	for id in race_ids:
		else_races_id = get_else_races_id(id)
		all_race_ids += else_races_id
	all_race_info = []
	for id in all_race_ids:
		race_info = Race(main_race_id)
		race_info.get_horse_info_in_race()
		all_race_info.append(race_info)
