// Angular Core
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { SessionDataInterface } from 'src/app/interfaces/session-data-interface';
import { DataTransmitterService } from 'src/app/services/dataTransmitter/data-transmitter.service';
import { SessionService } from 'src/app/services/session/session.service';

@Component({
  standalone: false,
  selector: 'app-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.scss']
})
export class FooterComponent implements OnInit, OnDestroy {

  session!: SessionDataInterface;
  sitenameListener!: Subscription;

  /**
   * private variable to generate current year
   * @type Number
   */
  currentYear = new Date().getFullYear();

  /**
   * data to display in footer
   * @type Object
   */
  footerData = {
      year: this.currentYear,
      sitename: '', // TODO: This needs to come from the database, not hardcoded in here.
      versionId: 0,
      regStep: 0
  }

  isProduction = false;
  displayAds:boolean = false;
  menuSwitches:any = {};
  menuSwitchesListener!: Subscription;


  constructor(
    private sessionService: SessionService,
    private dataTransmitter: DataTransmitterService
  ) { }

  ngOnDestroy(): void {
    this.sitenameListener.unsubscribe();
  }

  ngOnInit(): void {
    this.getVersionId();
    if (window.location.href.indexOf('trxadedev') === -1) {
      this.isProduction = true;
    }
    if (this.footerData.regStep === 6 && this.isProduction) {
      this.loadLinkedInInsightTag();
    }
    this.sitenameListener = this.dataTransmitter.sitename.subscribe((sitename) => {
      this.footerData.sitename = sitename;
    });
    this.menuSwitchesListener = this.dataTransmitter.menuSwitches.subscribe((menuSwitches) => {
        this.menuSwitches = menuSwitches;
        this.setDisplayAds();
    });
  }
  
  loadLinkedInInsightTag() {
    (window as any)._linkedin_partner_id = "6382681";
    (window as any)._linkedin_data_partner_ids = (window as any)._linkedin_data_partner_ids || [];
    (window as any)._linkedin_data_partner_ids.push((window as any)._linkedin_partner_id);

    if (document.getElementById('linkedin-insight-script')) return;

    const s = document.getElementsByTagName('script')[0];
    const b = document.createElement('script');
    b.type = 'text/javascript';
    b.async = true;
    b.src = 'https://snap.licdn.com/li.lms-analytics/insight.min.js';
    b.id = 'linkedin-insight-script';
    s.parentNode!.insertBefore(b, s);
  }

  /**
   * Show ads only if user is a registered buyer
   * @returns {Boolean}
   */
  setDisplayAds () {
      if(this.menuSwitches) {
          this.displayAds = (this.menuSwitches.isLoggedIn && this.menuSwitches.isBuyer && this.menuSwitches.isFinal);
      }
      return this.displayAds;
  };

  getVersionId() {
    if (typeof this.sessionService.getSession() === 'undefined') {
      this.sessionService.get().then((session: any) => {
        this.footerData.versionId = session.data.version_id;
        this.footerData.regStep = session.data.reg_step;
        this.dataTransmitter.sitename.next(session.data.sitename);
      });
    } else {
      this.footerData.versionId = this.sessionService.getSession().version_id;
      this.footerData.regStep = this.sessionService.getSession().reg_step;
      this.dataTransmitter.sitename.next(this.sessionService.getSession().sitename);
    }
  }
}
