package com.yutou.biliapi.databases;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.biliapi.bean.login.LoginCookieDatabaseBean;
import com.yutou.biliapi.bean.login.LoginUserDatabaseBean;
import com.yutou.biliapi.bean.user.UserInfoBean;
import com.yutou.bilibili.Tools.DateFormatUtils;
import com.yutou.common.databases.AbsDatabasesBean;
import com.yutou.common.databases.SQLiteManager;

import java.util.ArrayList;
import java.util.List;

public class BiliBiliLoginDatabase extends SQLiteManager {
    LoginCookieDatabaseBean cookie;
    private static BiliBiliLoginDatabase instance;

    private BiliBiliLoginDatabase(Class<LoginCookieDatabaseBean> tClass) {
        try {
            cookie = new LoginCookieDatabaseBean();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        init();
    }

    public static BiliBiliLoginDatabase getInstance() {
        if (instance == null) {
            instance = new BiliBiliLoginDatabase(LoginCookieDatabaseBean.class);
        }
        return instance;
    }


    public BiliBiliLoginDatabase initData(LoginCookieDatabaseBean cookie, LoginUserDatabaseBean user) {
        this.cookie = cookie;
        cookie.setTableName(getDataBean().get(0).getTableName());
        LoginCookieDatabaseBean old = getCookie(cookie.getDedeUserID());
        if (old == null) {
            add(cookie);
            add(user);
        }else{
            cookie.setSql_time(old.getSql_time());
            LoginUserDatabaseBean oldUser = getUser(cookie.getDedeUserID());
            user.setSql_time(oldUser.getSql_time());
            update(cookie);
            update(user);
        }
        return this;
    }

    public LoginCookieDatabaseBean getCookie(String userId) {
        List<LoginCookieDatabaseBean> list = super.get(cookie.getTableName(), LoginCookieDatabaseBean.class);
        if (userId == null && !list.isEmpty()) {
            return list.getFirst();
        }
        for (LoginCookieDatabaseBean bean : list) {
            if (bean.getDedeUserID().equals(userId)) {
                return bean;
            }
        }

        return null;
    }

    public List<LoginCookieDatabaseBean> getAllCookies() {
        return super.get(cookie.getTableName(), LoginCookieDatabaseBean.class);
    }

    public LoginUserDatabaseBean getUser(String userId) {
        List<LoginUserDatabaseBean> list = getAllUser();
        if (userId == null && !list.isEmpty()) {
            return list.getFirst();
        }
        for (LoginUserDatabaseBean bean : list) {
            if (bean.getUserInfo().getMid().equals(new String(userId))) {
                return bean;
            }
        }
        return null;
    }

    public List<LoginUserDatabaseBean> getAllUser() {
        JSONArray jsonArray = super.getJSONArray(new LoginUserDatabaseBean(null).getTableName());
        List<LoginUserDatabaseBean> list = new ArrayList<>();
        for (Object o : jsonArray) {
            JSONObject json = (JSONObject) o;
            LoginUserDatabaseBean bean = new LoginUserDatabaseBean(json.to(UserInfoBean.class));
            bean.setSql_time(DateFormatUtils.getInstance().parse(json.getString("sql_time"),DateFormatUtils.DEFAULT_PATTERN));
            list.add(bean);
        }
        return list;
    }

    @Override
    public String getFileName() {
        return "bilibili_login.db";
    }

    @Override
    protected List<AbsDatabasesBean> getDataBean() {
        return List.of(new LoginCookieDatabaseBean(), new LoginUserDatabaseBean(null));
    }

    public void updateLoginCookie(LoginCookieDatabaseBean cookie) {
        update(cookie);
    }
}
/**
 *
 */