import { stringify } from "querystring";
import { get } from "request";

import { Article } from "./types";

const URI = "https://api.cardmarket.com/ws/v2.0/output.json";

export class CardMarketApi {
  private AppToken: string;
  private AppSecret: string;
  private AccessToken: string;
  private AccessSecret: string;

  constructor() {
    this.AppToken = process.env.APP_TOKEN;
    this.AppSecret = process.env.APP_SECRET;
    this.AccessToken = process.env.ACCESS_TOKEN;
    this.AccessSecret = process.env.ACCESS_SECRET;
  }

  async get(path: string, params?: Record<string, any>): Promise<any> {
    const oauthParameters = {
      realm: `${URI}${path}`,
      consumer_key: this.AppToken,
      consumer_secret: this.AppSecret,
      token: this.AccessToken,
      token_secret: this.AccessSecret
    };
    return new Promise((resolve, reject) => {
      get(
        oauthParameters.realm + (params ? `?${stringify(params)}` : ""),
        {
          oauth: oauthParameters
        },
        (error, response) => {
          if (error) {
            throw error;
          } else if (response.statusCode > 299) {
            throw JSON.stringify({
              statusCode: response.statusCode,
              statusMessage: response.statusMessage
            });
          }
          try {
            resolve(JSON.parse(response.body));
          } catch (error) {
            reject(error);
          }
        }
      );
    });
  }

  async getAllArticles(uid: string): Promise<Article[]> {
    const perPage = 1000;
    let start = 0;
    let response;
    let data: Article[] = [];
    do {
      response = await this.get(`/users/${uid}/articles`, {
        start,
        maxResults: perPage
      });
      data = data.concat(response.article as Article[]);
      start += response.article.length;
    } while (response.article.length == perPage);
    return data;
  }
}
