import pandas as pd
import numpy as np
from time import sleep
from bs4 import BeautifulSoup
import re


def get_train_page(rid,driver):
    url = r"https://race.netkeiba.com/race/oikiri.html?race_id="+rid+r"&type=2"
    driver.get(url)
    sleep(2)
    soup = BeautifulSoup(driver.page_source,"html.parser")
    return soup

#　調教タイムリストをfloat arrayで返却
def cln_train_time(times: list):
    timesl=[]
    for time in times:
        if time != '' and time != '-' and '試' not in time and 'ゲート' not in time and '中間軽' not in time and '不' not in time and len(time) > 0:
            if '(' in time:
                # 先頭に（）
                # https://db.netkeiba.com/?pid=horse_training&id=2019110110
                if '(' == time[0]:
                    timesl.append(float(time[time.find(')')+1:]))
                else:
                    timesl.append(float(time[:time.find('(')]))
            else:
                # print(time.replace('ゲート',''))
                timesl.append(float(time.replace('ゲート','')))
    return np.array(timesl)

def comp_times(time1, time2, d1, d2):
    if len(time1) == 0 or len(time2) == 0:
        print('中間軽め','またはデータなし')
        return None
    else:
        print(d1['脚色'],d2['脚色'].values[0] ,
             d1['コース'],d2['コース'].values[0],
             d1['馬場'],d2['馬場'].values[0],
             d1['評価'],d2['評価'].values[0])
        Fcnt = len(time1) - len(time2)
        comptime=''
        if Fcnt > 0:
            comptime = time1[Fcnt:]-time2
            print(comptime,'タイム比較')
        else:
            comptime  = time1-time2[abs(Fcnt):]
            print(comptime,'タイム比較')
        if comptime.sum() == 0:
            print('調子　→')
        elif comptime.sum() > 0:
            print('調子　⤵')
        elif comptime.sum() < 0:
            print('調子　⤴')
    return comptime

def get_past_train_times(traindatakako: pd.DataFrame, traindatathistime: pd.DataFrame):
    前走 = traindatakako.head(1)
    好走BK = traindatakako[traindatakako['結果']==traindatakako['結果'].min()]
    好走BK2 = 好走BK[(好走BK['コース']==traindatathistime['コース'])&(好走BK['脚色']==traindatathistime['脚色'])]
    if len(好走BK2) > 0:
        # タイムで絞りたい
        print('条件一致１')
        好走 = 好走BK2.head(1)
    else:
        好走BK2 = 好走BK[(好走BK['コース']==traindatathistime['コース'])|(好走BK['脚色']==traindatathistime['脚色'])]
        if len(好走BK2) > 0:
            # タイムで絞りたい
            print('条件一致２')
            好走 = 好走BK2.head(1)
        else:
            好走 = 好走BK.head(1)
    return 前走, 好走


#　最終追切リストを取得　→　traindatas
def get_latest_train_list(soup):
    tbody = soup.find('tbody')
    trs = tbody.findAll('tr')
    ths = trs[0].findAll('th')
    headers = [th.text.strip().replace('ラップ表示','').replace('\n','') for th in ths]
    headers
    traindatas=[]
    for row in trs[1:]:
        rtds = row.findAll('td')
        traindata = {}
        for header,rtd in zip(headers,rtds):
            if header == '馬名':
                ass = rtd.findAll('a')
                horseid = ass[0].get('href')
                if len(ass) == 2:
                    zensou = ass[1].get('href')
                    traindata[header]=rtd.text.strip().replace('前走','').replace('\n','')
                    traindata['前走'] = zensou
                else:
                    traindata[header]=rtd.text.strip()
                traindata['hid'] = horseid

            elif '調教タイム' in header:
                lis = rtd.findAll('li')
                lis = [l.text for l in lis]
                traindata[header]=lis
            else:
                traindata[header]=rtd.text.strip()
        traindatas.append(traindata)
    return traindatas

def select_traindatas(url,driver):
    driver.get(url)
    sleep(2)
    flg = True
    traindata3=[]
    while flg:
        soup = BeautifulSoup(driver.page_source,"html.parser")
        ul = soup.find('ul',class_='table_list')
        tables = ul.findAll('table')
        for table in tables:
            cap = table.find('caption')
            tbody = table.find('tbody')
            rname = cap.text.strip().replace('\xa0',' ')
            retult = rname[rname.rfind('着')-2:].strip().replace('着','')
            retult
            trs = tbody.findAll('tr')
            ths = trs[0].findAll('th')
            headers = [th.text.strip().replace('ラップ表示','').replace('\n','') for th in ths]
            for row in trs[1:]:
                rtds = row.findAll('td')
                traindata2 = []
                traindata2.append(rname)
                traindata2.append(retult)
                for header,rtd in zip(headers,rtds):
                    if '調教タイム' in header:
                        lis = rtd.findAll('li')
                        lis = [l.text for l in lis]
                        traindata2.append(lis)
                    else:
                        traindata2.append(rtd.text.strip())
                traindata3.append(traindata2)
        nextp = driver.find_elements_by_xpath('//a[text()="次"]')
        if len(nextp) == 2:
            nextp[0].click()
            sleep(3)
        else:
            flg = False
    header = ['対象レース時','結果','日付', 'コース', '馬場', '乗り役', '調教タイム', '位置', '脚色', '評価', '映像']
    traindatakako = pd.DataFrame(traindata3,columns=header)
    traindatakako['結果'] = pd.to_numeric(traindatakako['結果'], errors='coerce')
    return traindatakako

def get_and_compare_past_train_times(traindatas, driver,f=None ):
    # traindatakako = ''
    for traindatathistime in traindatas:
        if '前走' in traindatathistime.keys():
            # select_traindatas(url,driver)
            try:
                traindatakako = select_traindatas(traindatathistime['前走'],driver)
            except:
                print('データなし')
                continue
            前走, 好走 = get_past_train_times(traindatakako, traindatathistime)
            print('-----------'+traindatathistime['馬番']+' '+traindatathistime['馬名']+'-----------')
            print('前走時比較-----------',str(前走['結果'].values[0])+'位')
            thistimetrain = cln_train_time(traindatathistime['調教タイム'])
            zensoutrain = cln_train_time(前走['調教タイム'].values[0])
            comp_time前走 = comp_times(thistimetrain,zensoutrain,traindatathistime,前走)
            print('好走時比較-----------',str(好走['結果'].values[0])+'位')
            kousoutrain = cln_train_time(好走['調教タイム'].values[0])
            comp_time好走 = comp_times(thistimetrain,kousoutrain,traindatathistime,好走)
    # return (traindatakako,comp_time前走,comp_time好走)

def cln_train_time3(time :str):
    if time == '-':
        return None
    flg = True
    while flg:
        l = time.find('(')
        r = time.find(')')
        # print(time)
        if l > 0 or r > 0:
            delstr = time[l:r+1]
            time = time.replace(delstr,'')
        else:
            flg = False
    m = re.compile('r[^0-9].')
    if m.search(time):
        print('err',time)
        return None
    time.replace('ラップ','')
    return time



def cln_train_time2(pd):
    times = pd['調教タイム'] # ['-', '(2)55.5', '39.5', '25.7', '12.4']
    if type(times) is str:
        times = eval(times)
    ttime = [np.nan,np.nan,np.nan,np.nan,np.nan]
    for i, time in enumerate(times) :
        time = cln_train_time3(time)
        if time:
            try:
                ttime[i] = float(time)
            except Exception as e:
                print(e)
                pd['調教タイム1'] = np.nan
                pd['調教タイム2'] = np.nan
                pd['調教タイム3'] = np.nan
                pd['調教タイム4'] = np.nan
                pd['調教タイム5'] = np.nan
                pd['調教タイム合計'] = np.nan
                return pd
    pd['調教タイム1'] = ttime[0]
    pd['調教タイム2'] = ttime[1]
    pd['調教タイム3'] = ttime[2]
    pd['調教タイム4'] = ttime[3]
    pd['調教タイム5'] = ttime[4]
    sum_time = [t for t in ttime if not np.isnan(t)]
    pd['調教タイム合計'] = sum(sum_time)
    return pd



def compare_standard_time(pd, pd_s):
    # print(pd)
    try:
        res = pd_s[(pd_s['コース']==pd['コース']) & (pd_s['脚色']==pd['脚色'])]
        time1 = time2 = time3 = time4 = time5 = 0
        if len(res) == 1:
            if not np.isnan(pd['調教タイム1']) and not np.isnan(res['調教タイム1'].values[0]):
                time1 = pd['調教タイム1'] - res['調教タイム1'].values[0]
            if not np.isnan(pd['調教タイム2']) and not np.isnan(res['調教タイム2'].values[0]):
                time2 = pd['調教タイム2'] - res['調教タイム2'].values[0]
            if not np.isnan(pd['調教タイム3']) and not np.isnan(res['調教タイム3'].values[0]):
                time3 = pd['調教タイム3'] - res['調教タイム3'].values[0]
            if not np.isnan(pd['調教タイム4']) and not np.isnan(res['調教タイム4'].values[0]):
                time4 = pd['調教タイム4'] - res['調教タイム4'].values[0]
            if not np.isnan(pd['調教タイム5']) and not np.isnan(res['調教タイム5'].values[0]):
                time5 = pd['調教タイム5'] - res['調教タイム5'].values[0]
            # print('平均比較',time1,time2,time3,time4,time5)
        pd['調教基準比較上り'] = time5
        調教基準比較 = [time1,time2,time3,time4,time5]
        調教基準比較 = [t for t in 調教基準比較 if t != 0]
        pd['調教基準比較'] = sum(調教基準比較)
        pd['調教基準比較平均'] = sum(調教基準比較) / len(調教基準比較)
    except TypeError as e:
        print(e)
        print(pd)
        pd['調教基準比較'] = 0
        pd['調教基準比較平均'] = 0
        pd['調教基準比較上り'] = 0
    except ZeroDivisionError :
        print('zero')
        pd['調教基準比較'] = 0
        pd['調教基準比較平均'] = 0
        pd['調教基準比較上り'] = 0
    return pd

traindata_compare = r"""
cd C:\Users\yohei\AppData\Local\Git\statistics\scraping\HorseRace
from HorseRace import common as c
from HorseRace import training as t
import pandas as pd
p = r'D:\horserace\data\ana\train_mean.csv'
cdf = pd.read_csv(p)

driver = c.get_driver()
c.set_login(driver)

何かしらレースを取得
rid = '202107060211'
soup = t.get_train_page(rid,driver)
traindatas = t.get_latest_train_list(soup)
traindatass = pd.DataFrame(traindatas)
traindatass = traindatass[['枠', '馬番','馬名', 'コース', '馬場','調教タイム', '脚色', '評価']]

traindatass = traindatass.apply(lambda x:t.cln_train_time2(x), axis=1)
traindatass2 = traindatass.apply(lambda x :t.compare_standard_time(x,cdf) , axis=1)
traindatass2

"""

traindata_get=r"""
from HorseRace import common as c
from HorseRace import training as t
driver = c.get_driver()
c.set_login(driver)

https://race.netkeiba.com/top/race_list.html # 手動で移動
driver.current_url

sp = c.get_pages(driver.current_url)
rs = driver.find_elements_by_class_name('RaceList_DataList')
rsnames = [rsn.find_element_by_class_name('RaceList_DataTitle').text for rsn in rs ]
print(rsnames)
rsnames = [r for r in rsnames if r !='']

racelist = []
for rss in rs:
    rsname = rss.find_element_by_class_name('RaceList_DataTitle').text
    rsis = rss.find_elements_by_class_name('RaceList_DataItem')
    for rsi in rsis:
        rsia = rsi.find_element_by_tag_name('a')
        rsin = rsi.find_element_by_class_name('Race_Num')
        rsi2 = rsi.find_element_by_class_name('RaceList_ItemContent')
        rsi2t = rsi2.find_element_by_class_name('ItemTitle')
        rsi2d = rsi2.find_element_by_class_name('RaceData')
        rsi2d2 = rsi2d.find_element_by_class_name('RaceList_Itemtime')
        rsi2d3 = rsi2d.find_elements_by_class_name('RaceList_ItemLong')
        if rsi2d3 is not None and rsi2d3 != []:
            rsi2d3 = rsi2d3[0].text
        else:
            rsi2d3 = '障害'
        racelist.append((rsname,rsin.text,rsi2t.text,rsi2d2.text,rsi2d3,rsia.get_attribute('href')))

racelist = [rrr for rrr in racelist if rrr[0] !='']

import pandas as pd
racelistpd = pd.DataFrame(racelist,columns=['会場','レースno','タイトル','発走時間','距離タイプ','URL'])
print(racelistpd[['会場','レースno','タイトル','発走時間','距離タイプ']])
racelistpd['URL'].values


rids = [select_rid(ridbk) for ridbk in racelistpd['URL'].values]

# for rid in rids:
rid = rids[0]
soup = t.get_train_page(rid,driver)
traindatas = t.get_latest_train_list(soup)
traindatakakos = ''
for j, traindatathistime in enumerate(traindatas):
    if '前走' in traindatathistime.keys():
        try:
            if j == 0:
                print('0')
                traindatakako = t.select_traindatas(traindatathistime['前走'],driver)
                traindatakakos = traindatakako.copy()
            else:
                traindatakakos = traindatakakos.append(t.select_traindatas(traindatathistime['前走'],driver))
        except Exception as e:
            print(e)
            continue




from time import sleep
for rid in rids[1:]:
    soup = t.get_train_page(rid,driver)
    try:
        traindatas = t.get_latest_train_list(soup)
    except:
        print('新馬戦？',rid)
    for j, traindatathistime in enumerate(traindatas):
        if '前走' in traindatathistime.keys():
            try:
                traindatakakos = traindatakakos.append(t.select_traindatas(traindatathistime['前走'],driver))
                sleep(2)
            except Exception as e:
                print(e)
                continue



traindatakakos
# traindatakakos = traindatakakos.apply(lambda x:cln_train_time20(x), axis=1)
traindatakakos = traindatakakos.apply(lambda x:t.cln_train_time2(x), axis=1)


traindatakakos[traindatakakos['対象レース時']=='2021/08/22  札幌1R  2歳未勝利  結果 ： 3着']

traindatakakos['レース'] = traindatakakos['対象レース時'].apply(lambda x:select_racename(x))
traindatakakos['グレード'] = traindatakakos['レース'].apply(categorize_races)

tg = traindatakakos.groupby(['コース','脚色'])['調教タイム1','調教タイム2','調教タイム3','調教タイム4','調教タイム5','調教タイム合計']
tg_mean = tg.mean()


from pathlib import Path
p=Path(r'D:\horserace\data\ana\trabk')

tg_mean.to_csv(p / "train_mean220122.csv")

datag = p.glob('train_mean*.csv')
for i,dataf in enumerate(datag):
    print(dataf.name)
    if i == 0:
        data = pd.read_csv(dataf)
    else:
        data = data.append(pd.read_csv(dataf))



data
# data = data.round(2)
tg = data.groupby(['コース','脚色'])['調教タイム1','調教タイム2','調教タイム3','調教タイム4','調教タイム5','調教タイム合計']
tg_mean = tg.mean()
tg_mean = tg_mean.round(2)
tg_mean.to_csv(r'D:\horserace\data\ana\train_mean.csv')

traindatakakos.to_csv(p / "rec" / "tra220122.csv")

"""
