import os
from HorseRace.common import *
from pathlib import Path
from urllib import request
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
from datetime import timedelta
import numpy as np
from urllib import parse
import csv

def get_pages(url):
	res = request.urlopen(url)
	soup = BeautifulSoup(res,"html.parser")
	res.close()
	return soup

def get_tables(url):
	data = pd.read_html(url)
	return data

def get_driver(opt=None):
	# dp = r"C:\driver\96\chromedriver.exe"
	options = Options()
	if opt:
		options.add_argument('--headless')
	return webdriver.Chrome(ChromeDriverManager().install(), options=options)
	# return webdriver.Chrome(dp, options=options)


def set_login(d):
	d.get(r"https://regist.netkeiba.com/account/?pid=login")
	# d.switch_to_window(d.window_handles[1])
	loginid = d.find_element_by_name("login_id")
	pswd = d.find_element_by_name('pswd')
	logbtn = d.find_element_by_class_name('loginBtn__wrap').find_element_by_tag_name('input')
	loginid.send_keys(os.environ['email'])
	pswd.send_keys(os.environ['passwd'])
	logbtn.click()
	return d


def categorize_races(rname):
	f = open(r"D:\horserace\data\race\mst\race.csv",encoding='utf8')
	if not f.exists():
		f = open(r"E:\horserace\data\race\mst\race.csv",encoding='utf8')
	txt = csv.reader(f)
	text = list(txt)
	G1,G2,G3=[],[],[]
	for t in text:
		if t[0] == 'G1':
			G1.append(t[1])
		elif t[0] == 'G2':
			G2.append(t[1])
		elif t[0] == 'G3':
			G3.append(t[1])
	category = ''
	if 'G1' in rname or rname in G1:
		category = '01.G1'
	elif 'G2' in rname or rname in G2:
		category = '02.G2'
	elif 'G3' in rname  or rname in G3:
		category = '03.G3'
	elif 'OP' in rname or 'S' in rname or 'ステークス' in rname or '賞' in rname or '杯' in rname or '特別' in rname :
		category = '04.OP'
	elif 'L' in rname:
		category = '05.L'
	elif '1600万下' in rname:
		category = '06.1600万下'
	elif '1000万下' in rname:
		category = '07.1000万下'
	elif '500万下' in rname:
		category = '08.500万下'
	elif '3勝クラス' in rname:
		category = '09.3勝クラス'
	elif '2勝クラス' in rname:
		category = '10.2勝クラス'
	elif '1勝クラス' in rname:
		category = '11.1勝クラス'
	elif '2歳未勝利' in rname:
		category = '12.2歳未勝利'
	elif '3歳未勝利' in rname:
		category = '13.3歳未勝利'
	elif '3歳以上未勝利' in rname:
		category = '13.3歳未勝利'
	elif '3歳新馬' in rname:
		category = '14.3歳新馬'
	elif '2歳新馬' in rname:
		category = '15.2歳新馬'
	else:
		print(rname, 'OP?')
		category = '04.OP'
	f.close()
	return category

def categorize_races_bk(rname):
	category = ''
	if '(G1)' in rname:
		category = '01.G1'
	elif '(G2)' in rname:
		category = '02.G2'
	elif '(G3)' in rname:
		category = '03.G3'
	elif '(OP)' in rname:
		category = '04.OP'
	elif '(L)' in rname:
		category = '05.L'
	elif '1600万下' in rname:
		category = '06.1600万下'
	elif '1000万下' in rname:
		category = '07.1000万下'
	elif '500万下' in rname:
		category = '08.500万下'
	elif '3勝クラス' in rname:
		category = '09.3勝クラス'
	elif '2勝クラス' in rname:
		category = '10.2勝クラス'
	elif '1勝クラス' in rname:
		category = '11.1勝クラス'
	elif '2歳未勝利' in rname:
		category = '12.2歳未勝利'
	elif '3歳未勝利' in rname:
		category = '13.3歳未勝利'
	elif '3歳新馬' in rname:
		category = '14.3歳新馬'
	elif '2歳新馬' in rname:
		category = '15.2歳新馬'
	return category



def split_distance(df):
	print(type(df),df['距離'])
	dtype = ''
	if isinstance(df['距離'], str):
		if df['距離'][0] == '芝':
			dtype = '芝'
		elif df['距離'][0] == 'ダ':
			dtype = 'ダ'
		elif df['距離'][0] == '障':
			dtype = '障'
	else:
		if df['距離'].str[0] == '芝':
			dtype = '芝'
		elif df['距離'].str[0] == 'ダ':
			dtype = 'ダ'
		elif df['距離'].str[0] == '障':
			dtype = '障'
		else:
			print('不明', df['距離'])
			return ''
	df['距離'] = df['距離'].replace('芝','').replace('ダ','').replace('障','')
	df['タイプ'] = dtype
	return df

def convert_time(time):
	m = int(time[0])
	s = int(time[2:4])
	ss = int(time[5])*100000
	td = timedelta(minutes=m,seconds=s,microseconds=ss).total_seconds()
	return td

def convert_time2(time):
	if type(time) is str:
		m = int(str(time[0]))
		s = int(str(time[2:4]))
		ss = int(str(time[5]))*100000
		td = timedelta(minutes=m,seconds=s,microseconds=ss).total_seconds()
	else:
		print(type(time),time)
		td = None
	return td

def convert_time3(time):
	if type(time) is str:
		# m = int(str(time[0]))
		s = int(str(time[0:2]))
		ss = int(str(time[3:4]))*100000
		td = timedelta(seconds=s,microseconds=ss).total_seconds()
	else:
		print(type(time),time)
		td = None
	return td

def get_all_horse_data():
	hp = Path(r"D:\horserace\data\horse")
	if not hp.exists():
		hp = Path(r"E:\horserace\data\horse")
	hds = hp.glob('*.csv')
	for i,hd in enumerate(hds):
		hdd = pd.read_csv(hd)
		hdd['hid'] = hd.name[:10]
		if i == 0:
			hdd2 = hdd.copy()
		else:
			hdd2 = hdd2.append(hdd)


	# Index(['Unnamed: 0', '日付', '開催', '天気', 'レース名', '距離', '頭数', '枠番', '馬番', '人気',
    #    '着順', '着差', '騎手', '斤量', 'タイム', '通過', 'ペース', '上り', '馬体重', '勝馬', 'hid',
    #    'Unnamed: 0.1'],
    #   dtype='object')
	hdd2 = hdd2.dropna(how='any')
	hdd2['開催'] = hdd2['開催'].replace('[0-9]','',regex=True)
	hdd2['グレード'] = hdd2['レース名'].apply(categorize_races)
	hdd2 = hdd2.apply(lambda x: split_distance(x), axis=1)
	# 距離　intに
	hdd2['time2'] = hdd2['タイム'].apply(convert_time)
	return hdd2

def select_rid(url):
	pu = parse.urlparse(url)
	ridd = pu.query.replace('race_id=','')[:12]
	print(ridd)
	return ridd

def select_racename(r):
    lpos = r.find('R  ')
    rpos = r.find('  結果')
    return r[lpos+3:rpos]

def select_hid(url):
	pu = parse.urlparse(url)
	hidd = pu.query.replace('id=','')[:10]
	print(hidd)
	return hidd
