import Scraper from './Scraper';
import Cache from '../util/Cache';
import Fetcher from '../api/Fetcher';

class Hack {
  scraper;
  cache;
  fetcher;
  api;

  constructor() {
    this.scraper = new Scraper();
    this.cache = new Cache();
    this.fetcher = new Fetcher(this.cache);
  }

  async getData(plate) {
    const cached = await this.cache.get(plate);
    if (cached) {
      console.log(`Using cached data for ${plate}`);
      return cached;
    }
    const data = await this.fetcher.init(plate);
    this.cache.save(plate, data);
    return data;
  }

  async init(plate) {
    try {
      const data = await this.getData(plate);
      this.scraper.setContent(data);
    } catch (e) {
      console.error(e);
      return;
    }
    this.scraper.scrapeBasicProperties();
    this.scraper.scrapeMainProperties();
    console.log(this.scraper.car);
  }

  getCar() {
    return this.scraper.car;
  }
}

export default Hack;
