#!/usr/bin/env python3.8
#!coding=utf-8
#
# 筛选excel数据 并画图
import re
import os
import pprint
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import time
import glob

#from f2_uncode_rawdata import readinRawDataJson
from f0_function import NormTimeNow, transtime
#import sys
#import sys
#import os
#filepath = os.path.split(os.path.realpath(__file__))[0]
#path0 = os.path.split(filepath)[0]
#sys.path.append(os.path.join(path0, 'DXY-2019-nCoV-Crawler'))
#import read2019nCoVdata as r2d
#from crawler_2019_nCoV_data import readindatalst as readdata
#import matplotlib as mpl
print("-"*10,"import all end","-"*10)
########


def autolabel(rects):
    """Attach a text label above each bar in *rects*, displaying its height."""
    # 柱状图加数值标签
    for rect in rects:
        height = rect.get_height()
        if height==0:
            continue
        plt.annotate(str(int(height)),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, -3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')
   #^^^^^^^END

def autolabel2(xlst, ylst, labellst):
    """Attach a text label above each bar in *rects*, displaying its height."""
    # copy from matplotlib.org
    for ix, x in enumerate(xlst):
#        height = ylst[ix]#'{}'.format(height),
        label = labellst[ix]#'{}'.format(height),
        if label==0:
            continue
        plt.annotate(str(int(label)),
                    xy=(x, ylst[ix]),
                    xytext=(10, -3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom',
                    bbox=dict(boxstyle='round, pad=0.1', 
                        fc='yellow', ec='k', lw=1, alpha=0.5))
   #^^^^^^^END


def transIntTostr(date1):
    # 20200202->'2020-02-02'
    return '-'.join([str(date1)[0:4], str(date1)[4:6], str(date1)[-2:]])
   #^^^^^^^END
def transfor(datat):
    # '2020-02-02' -> 20200202.0
    return int(datat.replace('-', ''))
   #^^^^^^^END


def findDataPath(startT, endT, datapath):
    # 输入xxxx-xx-xx格式的时间起止点，输出已有数据的时间格式列表
    def transforPath(dlst):
        # 将int转为地址
        dataS = [transIntTostr(i) for i in dlst]
        plst = [os.path.join(datapath, 'oneday-'+i+'.csv') for i in dataS]
        return plst
       #^^^^^^^END
    def datafile(datapath):
        # 所有数据文件的日期
        allfile = glob.glob(os.path.join(datapath, 'oneday-*.csv'))
        alldata = [i.split('oneday-')[1].split('.csv')[0] for i in allfile]
        allint = [transfor(i) for i in alldata]
        return sorted(allint)
       #^^^^^^^END
    def compare1(startTint, endTint, alllst):
            # []123
            # 123[]
            # [12]3
            # 1[23]
        if alllst[0]>=endTint:
            return alllst[:15]
        if alllst[-1] <= startTint:
            return alllst[-15:]
        dn1 = 0
        while startTint > alllst[dn1] and dn1<len(alllst)-1:
            dn1 += 1
        dn2 = -1
        while endTint < alllst[dn2] and dn2>-len(alllst):
            dn2 -= 1
        clst0 = alllst[dn1:dn2]
        clst0.append(alllst[dn2])
        return clst0
       #^^^^^^^END

    alllst = datafile(datapath)
    clst = compare1(transfor(startT), transfor(endT), alllst)
    return clst, transforPath(clst)
   #^^^^^^^END


def mergecsv(pathlst):
    pd1 = pd.read_csv(pathlst[0])
    for pdIpath in pathlst[1:]:
        pd1 = pd.concat(
                [pd1, 
                    pd.read_csv(pdIpath)]
                )
    return pd1.reset_index(drop=True)
   #^^^^^^^END


def plotTimeSeries(tlst, pathlst, pro):
    # 单省， 多日，全省趋势，各辖区趋势
    allpd = mergecsv(pathlst)
    print(tlst)
    datalst = [transIntTostr(i1)+'B' for i1 in tlst]
    ######## 图1数据: 各区新增时序
    cond0 = "provinceShortName=='"+pro+"'" +\
            " and date in "+ str(datalst)
    aimdata0 = allpd.query(cond0).copy() #避免链式索引
    # 修改列的属性为int
    aimdata0['yesterdayLocalConfirmedCount'] = \
        aimdata0['yesterdayLocalConfirmedCount'].astype('int64')
    aimdata0['yesterdayAsymptomaticCount'] = \
        aimdata0['yesterdayAsymptomaticCount'].astype('int64')
    # 将确诊和无症状列求和
    aimdata0['sumnew'] = aimdata0[
            ['yesterdayLocalConfirmedCount', 'yesterdayAsymptomaticCount']
            ].sum(axis=1)
    # 增加真实时间int便于排序
    # 2020-22-22B -> 2020-02-02 -> 10000000.0000
    realdateS = aimdata0['date'].copy()
    for indexi in realdateS.index:
        realdateS.loc[indexi] = transtime(realdateS.loc[indexi].replace('B', ''))
    aimdata0['realdate'] = realdateS
    aimdata0['realdate'] = aimdata0['realdate'].astype('int64')
#    print(aimdata1)
    # 区列表
    cond1 = "cityName!='省'"
    aimdata1 = aimdata0.query(cond1).copy() #避免链式索引
    allcitylst = set(aimdata1['cityName'].tolist())
#    print(allcitylst)
    # 标签
    nn = 3
    singleD = aimdata1.loc[aimdata1.date==transIntTostr(tlst[-1])+'B'].copy()
    singleD = singleD.sort_values(by=['sumnew'], ascending=False)
    singleLy = singleD['sumnew'].astype('int').tolist()[:nn]
    singleLs = singleD['cityName'].tolist()[:nn]
    ######## 图1
    fig = plt.figure(figsize=(12,6))
    ########
    plt.subplot(121)
    plt.subplots_adjust(bottom=0.05, top=0.9, left=0.07, right=0.98, hspace=0.3)
    plt.title(pro+'近期新增+无症状趋势'+'\n'+str(tlst[0])+'->'+str(tlst[-1]))
    #
    for cityI in allcitylst:
        cityD = aimdata1.loc[aimdata1.cityName==cityI].copy()
        cityD = cityD.sort_values(by=['realdate'], ascending=True)
        Xi = cityD['realdate'].tolist()
        XiLabel = [i1.replace('B', '')[5:] for i1 in cityD['date'].tolist()]
        Yi = cityD['sumnew'].astype('int').tolist()
        #
        plt.plot(Xi, Yi, '.-',  label=cityI)
    # 标签
    for i1 in range(len(singleLy)):
        plt.annotate(singleLs[i1]+','+str(singleLy[i1]),
                xy=(Xi[-1], singleLy[i1]), xytext=(10,5),
                textcoords="offset points",
                ha='right', va='bottom',
                bbox=dict(boxstyle='round, pad=0.1', 
                        fc='yellow', ec='k', lw=1, alpha=0.5)
                )
    #
    plt.xticks(Xi, XiLabel)
    plt.rcParams['font.sans-serif']=['SimHei']
    plt.legend(loc=1)
    ######## 图2数据 province的新增时间变化
    cond2 = "cityName=='省'"
    aimdata2 = aimdata0.query(cond2).copy() #避免链式索引
    aimdata2 = aimdata2.sort_values(by=['realdate'], ascending=True)
    X2 = aimdata2['realdate'].tolist()
    X2Label = [i1.replace('B', '')[5:] for i1 in cityD['date'].tolist()]
    Y21 = aimdata2['yesterdayLocalConfirmedCount'].astype('int').tolist()
    Y22 = aimdata2['yesterdayAsymptomaticCount'].astype('int').tolist()
    ########
    plt.subplot(122)
    plt.subplots_adjust(bottom=0.05, top=0.9, left=0.07, right=0.98, hspace=0.3)
    plt.title(pro+'近期新增+无症状趋势'+'\n'+str(tlst[0])+'->'+str(tlst[-1]))
    b1 = plt.bar(X2, Y21, label='确诊')
    b2 = plt.bar(X2, Y22, bottom=Y21, label='无症状')
    autolabel(b1)
    autolabel2(X2, np.array(Y21)+np.array(Y22), Y22)
    plt.xticks(X2, XiLabel)
    plt.rcParams['font.sans-serif']=['SimHei']
    plt.legend(loc=1)
    ########
#    plt.show()
   #^^^^^^^END


def plot1pro1(time1, proname):
    # 某日，某省，新增确诊和总确诊柱状图
    # 当日的静态数据
    dataall = pd.read_csv(os.path.join(datafile, 'oneday-'+time1+'.csv'))
    ########A 总数据，B新增数据
    ######################## 图1:当日新增
    cond1 = "provinceShortName=='"+proname+"'"+\
           " and date=='"+time1+"B'" + \
           " and cityName!='省'"
    aimdata1 = dataall.query(cond1)
    ########
    X1 = aimdata1['cityName'].tolist()
    Y1 = aimdata1['yesterdayLocalConfirmedCount'].astype('int').tolist()
    Y2 = aimdata1['yesterdayAsymptomaticCount'].astype('int').tolist()
    cond1 = "provinceShortName=='"+proname+"'"+\
           " and date=='"+time1+"B'" + \
           " and cityName=='省'"
    labeldata1 = dataall.query(cond1)['yesterdayLocalConfirmedCount'].tolist()[0]
    labeldata2 = dataall.query(cond1)['yesterdayAsymptomaticCount'].tolist()[0]
    ######## f1 省内当日各区确诊情况
    fig = plt.figure(figsize=(12,6))
    ########
    plt.subplot(211)
    plt.subplots_adjust(bottom=0.05, top=0.95, left=0.07, right=0.98, hspace=0.3)
    plt.title(time1+' '+proname+'当日新增('+labeldata1+'+'+labeldata2+')')
    b1 = plt.bar(X1, Y1, label='确诊')
    b2 = plt.bar(X1, Y2, bottom=Y1, label='无症状')
    autolabel(b1)
    autolabel2(X1, np.array(Y1)+np.array(Y2), Y2)
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.legend()
    ######################## 图2:现存确诊
    cond2 = "provinceShortName=='"+proname+"'"+\
           " and date=='"+time1+"A'" + \
           " and cityName!='省'"
    aimdata2 = dataall.query(cond2)
    aimdata2 = aimdata2[aimdata2['currentConfirmedCount']>0]
#    print(aimdata2['currentConfirmedCount'].dtypes)
#    print(aimdata2.query('currentConfirmedCount>1'))
#    print(aimdata2.dtypes)
    ########
    X2 = aimdata2['cityName'].tolist()
    Y3 = aimdata2['currentConfirmedCount'].astype('int').tolist()
    cond2 = "provinceShortName=='"+proname+"'"+\
           " and date=='"+time1+"A'" + \
           " and cityName=='省'"
    labeldata2 = dataall.query(cond2)['currentConfirmedCount'].tolist()[0]
    ########
    plt.subplot(212)
    plt.subplots_adjust(bottom=0.05, top=0.95, left=0.07, right=0.98, hspace=0.3)
    plt.title(time1+' '+proname+'现存确诊'+str(labeldata2))
    b3 = plt.bar(X2, Y3)
    autolabel(b3)
    plt.rcParams['font.sans-serif'] = ['SimHei']
    ########
#    plt.show()
   #^^^^^^^END


########################
if __name__ == '__main__':
    datafile = '/Users/shzy/2019nCov-dataset'
    pro = '北京'
    ######## 单省-单日数据
    todayT = NormTimeNow()
    plot1pro1(todayT, pro)
    ######## 单省-多日数据
    timedel = 15
    endT = NormTimeNow()
    startT = NormTimeNow(time.time()-timedel*60*60*24)
#    startT = '2022-05-26'
#    endT = '2022-05-27'
    tlst, pathlst = findDataPath(startT, endT, datafile)
    plotTimeSeries(tlst, pathlst, pro)
    plt.show()
