from datetime import datetime, timedelta

from ccxt import binance
from notecoin.coins.base.file import DataFileProperty
from notecoin.task import BaseTask


class LoadTask(BaseTask):
    table_name = 'load'

    def __init__(self, *args, **kwargs):
        self.path_root = '/home/bingtao/workspace/tmp'
        super(LoadTask, self).__init__(*args, **kwargs)

    @staticmethod
    def parse_day(ds):
        ds = ds
        first = datetime.strptime(ds, '%Y-%m-%d') - timedelta(days=1)
        last = first + timedelta(days=1) - timedelta(seconds=1)
        return first, last

    @staticmethod
    def parse_week(ds, weeks=0):
        ds = ds
        first = datetime.strptime(ds, '%Y-%m-%d') + timedelta(weeks=weeks)
        first = first - timedelta(days=first.weekday())
        first = datetime(first.year, first.month, first.day)
        last = first + timedelta(weeks=1) - timedelta(seconds=1)
        return first, last


class LoadKlineDailyTask(LoadTask):
    def refresh(self, ds):
        start, end = self.parse_day(ds)
        file_pro = DataFileProperty(exchange=binance(), path=self.path_root)
        file_pro.file_format = '%Y%m%d'
        file_pro.change_data_type('kline')
        file_pro.change_timeframe('1m')
        file_pro.change_freq('daily')
        file_pro.load_daily(start, end)


class LoadKlineWeeklyTask(LoadTask):
    def refresh(self, ds):
        start, end = self.parse_week(ds, weeks=-1)
        file_pro = DataFileProperty(exchange=binance(), path=self.path_root)
        file_pro.file_format = '%Y%m%d'
        file_pro.change_data_type('kline')
        file_pro.change_timeframe('1m')
        file_pro.change_freq('weekly')
        print((start, end))
        file_pro.load_merge(start, end)


class LoadTradeDailyTask(LoadTask):
    def refresh(self, ds):
        start, end = self.parse_day(ds)
        file_pro = DataFileProperty(exchange=binance(), path=self.path_root)
        file_pro.file_format = '%Y%m%d'
        file_pro.change_data_type('trade')
        file_pro.change_timeframe('detail')
        file_pro.change_freq('daily')
        file_pro.load_daily(start, end)


#LoadKlineWeeklyTask().refresh('2021-9-14')
