import {UserId, XAuthorization} from '@caj/configs/types';
import {makeRequest, apiBackendRequest} from '@caj/helper/request';
import BigDataManager from '@caj/helper/storage/BigDataManager';
import {store} from '@caj/redux/store';
import {createUserProp, SourceProp, User} from './types';

async function getUser(
  UserId: UserId,
  save?: boolean,
): Promise<User | undefined> {
  let user: User | undefined;

  let state = store.getState();

  if (UserId === state.appVariables.preferences.selectedAccount) {
    const usr = state.appVariables.preferences.accounts[UserId];

    if (usr !== undefined) {
      user = {
        AccountName: usr.AccountName,
        Description: usr.Description,
        FollowersCount: usr.FollowersCount,
        FollowingCount: usr.FollowingCount,
        lastUpdateTimestamp: usr.lastUpdateTimestamp,
        ProfilePicture: usr.ProfilePicture,
        UserId,
        Username: usr.Username,
        XpLevel: usr.XpLevel,
        XpPoints: usr.XpPoints,
      };
    }
  }

  if (user === undefined) {
    const usr = await BigDataManager.getEntry('users', UserId);

    user = {
      AccountName: usr.AccountName,
      Description: usr.Description,
      FollowersCount: usr.FollowersCount,
      FollowingCount: usr.FollowingCount,
      lastUpdateTimestamp: usr.lastUpdateTimestamp,
      ProfilePicture: usr.ProfilePicture,
      UserId,
      Username: usr.Username,
      XpLevel: usr.XpLevel,
      XpPoints: usr.XpPoints,
    };
  }

  if (user === undefined) {
    try {
      const resp = await makeRequest({
        path: apiBackendRequest.GET_USER_PROFILE,
        requestGET: {':userId': UserId},
        response: {
          AccountName: '',
          Description: '',
          FollowersCount: 0,
          FollowingCount: 0,
          Username: '',
          XpLevel: 0,
          XpPoints: 0,
          AvatarUrl: '',
        },
      });

      user = {
        AccountName: createUserProp(
          SourceProp.cached,
          resp.response.AccountName,
        ),
        Description: createUserProp(
          SourceProp.cached,
          resp.response.Description,
        ),
        FollowersCount: createUserProp(
          SourceProp.cached,
          resp.response.FollowersCount,
        ),
        FollowingCount: createUserProp(
          SourceProp.cached,
          resp.response.FollowingCount,
        ),
        lastUpdateTimestamp: Math.floor(new Date().getTime() / 1000),
        ProfilePicture: {
          hq: createUserProp(SourceProp.online, resp.response.AvatarUrl),
          lq: createUserProp(SourceProp.online, resp.response.AvatarUrl),
        },
        UserId,
        Username: createUserProp(SourceProp.offline, resp.response.Username),
        XpLevel: createUserProp(SourceProp.cached, resp.response.XpLevel),
        XpPoints: createUserProp(SourceProp.cached, resp.response.XpPoints),
      };

      BigDataManager.setEntry('users', user);
    } catch (error: any) {
      console.error(error.status);
    }
  }

  return user;
}

const UserManager = {getUser};
export default UserManager;
