from datetime import datetime
import time
from config import *

from monitor_min import Monitor

class MonitorCoin (Monitor):
    """자산(코인/주식/ETF) 모니터링 및 매수 실행 클래스"""

    def __init__(self, cooldown_file: str = './resources/coins_buy_time.json') -> None:
        super().__init__(cooldown_file)

    def monitor_coins(self) -> None:
        tmps = self.getBalances()
        balances = {}
        for tmp in tmps:
            balances[tmp['currency']] = {'balance': float(tmp['balance']), 'avg_buy_price': float(tmp['avg_buy_price'])}

        for symbol in KR_COINS_1:

            print("[{}] {}".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), symbol))
            interval = 1440
            data = self.get_coin_some_data(symbol, interval)
            data = self.calculate_technical_indicators(data)
            recent_data = self.annotate_signals(symbol, data)
            if recent_data['point'].iloc[-1] == 1:

                interval = 60
                data = self.get_coin_some_data(symbol, interval)
                if data is not None and not data.empty:
                    try:
                        inverseData= self.inverse_data(data)
                        recent_inverseData = self.annotate_signals(symbol, inverseData)
                        _ = self.buy_sell_ticker_1h(symbol, recent_inverseData, balances=balances, is_inverse=True)

                        data = self.calculate_technical_indicators(data)
                        recent_data = self.annotate_signals(symbol, data)

                        _ = self.buy_sell_ticker_1h(symbol, recent_data, balances=None, is_inverse=False)
                    except Exception as e:
                        print(f"Error processing data for {symbol}: {str(e)}")
                else:
                    print(f"Data for {symbol} is empty or None.")

            time.sleep(1)

        return
    # ------------- Scheduler -------------
    def run_schedule(self) -> None:

        while True:
            self.monitor_coins()
            time.sleep(3)

if __name__ == "__main__":
    MonitorCoin(cooldown_file='./resources/coins_buy_time_1h_1.json').run_schedule()
