import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { GetHistoryCommand } from '../commands/nonstophome/getHistoryCommand';
import { GetNonstopHomeEarnCommand } from '../commands/nonstophome/getNonstopHomeEarnCommand';
import { GetNonstopHomeHowItWorksCommand } from '../commands/nonstophome/getNonstopHomeHowItWorksCommand';
import { NonstopEarnResult } from '../models/nonstophome/nonstopEarnResult';
import { NonstopHistoryInput } from '../models/nonstophome/nonstopHistoryInput';
import { NonstopHistoryResult } from '../models/nonstophome/nonstopHistoryResult';
import { NonstopHomeTab } from '../models/nonstophome/nonstophometab';
import { NonstopHowItWorksResult } from '../models/nonstophome/nonstopHowItWorksResult';
import { EnterCodeTaskInput } from '../models/tasks/enterCodeTaskInput';
import { PerformTaskResult } from '../models/tasks/performTaskResult';
import { TaskType } from '../models/tasks/taskType';
import NonstopHome from '../views/nonstophome/NonstopHome';
import { BaseService } from './abstract/baseService';
import dataService from './dataService';
import userService from './userService';

class NonstopHomeService extends BaseService {
  public static sharedInstance = new NonstopHomeService();

  protected logTag: string = 'NonstopHomeService';

  private earnResult: NonstopEarnResult | null;
  private userHistory: NonstopHistoryResult | null;
  private howItWorks: NonstopHowItWorksResult | null;
  private element: HTMLElement;
  private baseUrl: string;

  public async presentNonstopHome(inElement: HTMLElement, withBaseUrl: string) {
    this.userHistory = await this.loadHistoryDetails();
    this.earnResult = await this.loadEarnDetails();
    this.howItWorks = await this.loadHowItWorksDetails();
    this.element = inElement;
    this.baseUrl = withBaseUrl;

    this.renderNonstopHome();
  }

  private async checkCode(code: string): Promise<PerformTaskResult> {
    const codeTaskInput = new EnterCodeTaskInput(TaskType.EnterCode);
    codeTaskInput.code = code;
    return await userService.userPerformedTask(codeTaskInput);
  }

  private async refreshTab(tab: NonstopHomeTab) {
    switch (tab) {
      case NonstopHomeTab.Earn:
        this.earnResult = await this.loadEarnDetails();
        break;
      case NonstopHomeTab.History:
        this.userHistory = await this.loadHistoryDetails();
        break;
      case NonstopHomeTab.HowItWorks:
        this.howItWorks = await this.loadHowItWorksDetails();
        break;
    }
    this.renderNonstopHome();
  }

  private async loadEarnDetails(): Promise<NonstopEarnResult | null> {
    const earnCommand = new GetNonstopHomeEarnCommand();
    return await dataService.executeCommand(earnCommand);
  }

  private async loadHistoryDetails(): Promise<NonstopHistoryResult | null> {
    const nonstopHistoryInput = new NonstopHistoryInput(new Date().getTimezoneOffset() * 60 * -1);
    const historyCommand = new GetHistoryCommand(nonstopHistoryInput);
    return await dataService.executeCommand(historyCommand);
  }

  private async loadHowItWorksDetails(): Promise<NonstopHowItWorksResult | null> {
    const howItWorksCommand = new GetNonstopHomeHowItWorksCommand();
    return await dataService.executeCommand(howItWorksCommand);
  }

  private renderNonstopHome() {
    if (this.userHistory && this.earnResult && this.howItWorks) {
      ReactDOM.render(
        <BrowserRouter basename={`/${this.baseUrl}`}>
          <NonstopHome
            userHistory={this.userHistory}
            earnResult={this.earnResult}
            howItWorks={this.howItWorks}
            checkCode={this.checkCode}
            refreshTab={(newTab) => this.refreshTab(newTab)}
            baseUrl={this.baseUrl}
          />
        </BrowserRouter>,
        this.element,
      );
    }
  }
}

export default NonstopHomeService.sharedInstance as NonstopHomeService;
