import hashlib
import json

import requests

from .base_loader import BaseLoader
from .config import (
    CICHANG_CLAENDAR_URL,
    CICHANG_COVERT_URL,
    CICHANG_LOGIN_URL,
    HJ_APPKEY,
    TIME_ZONE,
)


class CiChangLoader(BaseLoader):
    def __init__(self, from_year, to_year, **kwargs):
        super().__init__()
        self.from_year = from_year
        self.to_year = to_year
        self.user_name = kwargs.get("cichang_user_name", "")
        self.password = kwargs.get("cichang_password", "")
        self.s = requests.Session()
        self._make_years_list()

    @staticmethod
    def _md5_encode(string):
        m = hashlib.md5()
        m.update(string.encode())
        return m.hexdigest()

    def login(self):
        password_md5 = self._md5_encode(self.password)
        r = self.s.get(
            CICHANG_LOGIN_URL.format(user_name=self.user_name, password=password_md5)
        )
        if not r.ok:
            raise Exception(f"Someting is wrong to login -- {r.text}")
        club_auth_cookie = r.json()["Data"]["Cookie"]
        data = {"club_auth_cookie": club_auth_cookie}
        headers = {"hj_appkey": HJ_APPKEY, "Content-Type": "application/json"}
        # real login to get real token
        r = self.s.post(CICHANG_COVERT_URL, headers=headers, data=json.dumps(data))
        if not r.ok:
            raise Exception(f"Get real token failed -- {r.text}")
        data = r.json()["data"]
        access_token = data["access_token"]
        user_id = data["user_id"]
        headers["Access-Token"] = access_token
        self.s.headers = headers
        self.user_id = user_id

    def get_api_data(self):
        month_list = self.make_month_list()
        data_list = []
        for m in month_list:
            r = self.s.get(
                CICHANG_CLAENDAR_URL.format(
                    user_id=self.user_id,
                    start_date=m.to_date_string(),
                    end_date=m.end_of("month").to_date_string(),
                )
            )
            if not r.ok:
                print(f"get cichang calendar api failed {str(r.text)}")
            try:
                data_list.extend(r.json()["data"]["studyCountDays"])
            except:
                # just pass for now
                pass
        return data_list

    def make_track_dict(self):
        data_list = self.get_api_data()
        for d in data_list:
            if d:
                number = d["studyCount"]
                if number:
                    self.number_by_date_dict[d["studyDate"].replace("/", "-")] = number
                    self.number_list.append(number)

    def get_all_track_data(self):
        self.login()
        self.make_track_dict()
        self.make_special_number()
        return self.number_by_date_dict, self.year_list
