import { Injectable } from '@angular/core';
import {SessionDataProviderService} from "./session-data-provider.service";
import {WrenchService} from "../services/wrench.service";
import {Router} from "@angular/router";

@Injectable({
  providedIn: 'root'
})
export class BannersDataService {
  homebannerCount:number=0;
  bannerResult:any;
  bannerData: [];

  constructor(
      public sessionDataProviderService: SessionDataProviderService,
      private wrenchService: WrenchService,
      private route:Router
      )
  {

  }

  usrData: {
        action:number,
        member_id: number,
        uid: string,
        sessionid: string,
        limit: 30,
        offset: 0
    };

  async getBannersData(){
    this.usrData = {
        action:11200,
        member_id: this.sessionDataProviderService.member_id,
        uid: this.sessionDataProviderService.member_uid,
        sessionid: this.sessionDataProviderService.session,
        limit: 30,
        offset: 0
    };

      this.wrenchService.getHomeBanners(this.usrData).subscribe(
        bannerResult => {
          this.bannerResult = bannerResult;

            if ( this.bannerResult == undefined || this.bannerResult.internal_return == -9999){
             // force logout
                console.log("FORCE BANNER LOGOUT BANNERS RETURN->", this.bannerResult);
                this.sessionDataProviderService.DestroySessionOnLogout();
                this.route.navigate(['logout']);
            }

          console.log("BANNERS RETURN->", this.bannerResult);
          this.bannerData = this.bannerResult.result_list;
          this.homebannerCount = this.bannerData?.length;
        }
    );
  }

}
