import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { SessionDataProviderService } from 'src/app/store/session-data-provider.service';
import { BlogDataService } from 'src/app/store/blog-data.service';
import { WrenchService } from 'src/app/services/wrench.service';
import { OnesignalService } from 'src/app/services/onesignal.service';//
import { UserWalletService } from 'src/app/store/user-wallet.service';
import { LocTrcService } from "../../services/loc-trc.service";
import {MarketDataService} from "../../store/market-data.service";
import {BannersDataService} from "../../store/banners-data.service";

//import * as stream from "stream";

@Component({
  selector: 'app-home',
  templateUrl: './home.page.html',
  styleUrls: ['./home.page.scss'],
})
export class HomePage implements OnInit {
  homebannerCount:number=0;
  firstname: string = '';
  active_job_count:number = 0;
  blogData: [];
  accountType:string="";
  profilePicture = "https://www.wrenchboard.com/assets/images/profile.jpg";
  offerBanner = "https://www.wrenchboard.com/assets/images/apps/offer.jpg";
  walletDescription:string ="";
  walletData:any;
  enableTracking:number=0;


  constructor(
    private router: Router,
    public sessionDataProviderService: SessionDataProviderService,
    private wrenchService: WrenchService,
    private onesignalService: OnesignalService,
    public blogDataService: BlogDataService,
    public userWalletService: UserWalletService,
    private locTrcService:LocTrcService,
    public marketDataService:MarketDataService,
    public bannersDataService:BannersDataService
  ) {
    this.accountType = sessionDataProviderService.account_type;
    this.firstname = this.sessionDataProviderService.firstname;
    this.blogData = this.blogDataService.blogData;
    this.active_job_count =  this.sessionDataProviderService.active_job_count;
    this.enableTracking = this.sessionDataProviderService.tracking;
  }

  ngOnInit() {
    if ( this.sessionDataProviderService.member_uid != ''){
      console.log("BLOG HOME 002->", this.blogDataService.blogData);
      // this.firstname = this.sessionDataProviderService.firstname;
       this.onesignalService.Init(this.sessionDataProviderService.member_uid);
      this.locTrcService.startTracking();
      if (this.accountType == 'FULL'){
        this.getBlogData([]);
        this.getBannersData();
      }
      if (this.accountType == 'FAMILY'){
       // no need moved to components  this.getFamilySuggestList();
      }
    }

//debugger;
    this.profilePicture =  this.sessionDataProviderService.profilePicture(this.sessionDataProviderService.session, this.sessionDataProviderService.member_uid);

    this.walletPresentation();
  }
  ngOnInit() {
    this.allInitFunctions();


  }

  ionViewWillEnter(){
    console.log('Home page did enter - > ionViewWillEnter');
    this.allInitFunctions();
  }
  async walletPresentation(){
    this.walletData =  this.userWalletService.getWallet();
    if( this.walletData?.length > 0 ) {
      var num = new Number(this.walletData[0].amount*0.01);
      //this.fee_display = num.toFixed(2); //outputs 14
      this.walletDescription = num.toFixed(2) + ' ' + this.walletData[0].description;
    }
  }
  goPlayground(){
    this.router.navigate(['playground']);
  }
  ionViewDidEnter(){
    if (this.accountType == 'FULL'){
      this.getBlogData([]);
      this.getBannersData();
    }

    if (this.accountType == 'FAMILY'){
      this.getFamilySuggestList();
    }

    this.walletPresentation();
    //debugger;
  }
  blogResult:any;
  bannerResult:any;
  bannerData: [];
  getBlogData(blogReq=[]){
    this.wrenchService.getBlogData(blogReq).subscribe(
      blogResult => {
        this.blogResult = blogResult;
        console.log("BLOG RETURN->", this.blogResult);
        this.blogData = this.blogResult.blogdata.slice(0, 2);
       // debugger;
        this.blogDataService.setBlogData(this.blogResult.blogdata);
      }
    ); 
  }

  // 'member_id' => int 1
  // 'sessionid' => string '6AD58B08A9F458E6E6819DD603E202C657DB9BFFDD956C16386CC65975CE0D53' (length=64)
  // 'uid' => string '3119b744-42ad-4834-bb83-b737588754ca' (length=36)
  // 'action' => int 11200
  //

  async refreshBannersData(){
    this.bannerData = this.bannersDataService.bannerData;
    this.homebannerCount = this.bannersDataService.homebannerCount;
  }

  usrData: {
   action:number, member_id: number, uid: string,
    sessionid: string,
    'limit': 30,
    'offset': 0
  };
  async getBannersData(){
    this.refreshBannersData();

    var interval = setInterval( ()=> {
      this.refreshBannersData();
    }, 5000);


    // 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;
    //       console.log("BANNERS RETURN->", this.bannerResult);
    //       this.bannerData = this.bannerResult.result_list;
    //       this.homebannerCount = this.bannerData.length;
    //      // debugger;
    //       console.log("BANNERS RETURN DATA->", this.bannerData);
    //     //  this.blogDataService.setBlogData(this.blogResult.blog_data);
    //     }
    // );
  }

  familySuggestResult:any;
  familySuggestData:any;
  getFamilySuggestList(){
    this.usrData = {
      action:13010,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session,
      'limit': 30,
      'offset': 0
    };
    this.wrenchService.familySuggestList(this.usrData).subscribe(
        familySuggestResult => {
          this.familySuggestResult = familySuggestResult;
          console.log("familySuggestResult RETURN->", this.familySuggestResult);
          this.familySuggestData = this.familySuggestResult.result_list;
        }
    );
  }
  onDetails(item) {
   // var pageToNavigate = 'detail';
    var pageToNavigate = '';
    // alert(item.link_path);
      switch (item.link_path){
        case 'referral':
          pageToNavigate = 'invite';
          break;
        case 'mytask':  // offers waiting to start
          pageToNavigate = 'joboffers';
          break;

        case 'my-coupon':
          pageToNavigate = 'mycoupons';
          break;
        case 'blog-page':
        //  this.blogDetails(this.getOneBlog(item));
           this.getOneBlog(item);
          break;

        case 'my-review-jobs':
          pageToNavigate = 'jobmanager';
          break

        case 'my-pastdue-jobs':
          pageToNavigate = 'jobmanager';
          break

        case 'pend-interest':
          pageToNavigate ='pendinterest';
          break;



          case 'offer-interest':
            pageToNavigate = 'jobinterest';
          break

        case 'acc-family':
          pageToNavigate = 'family';
          break;

      }
      if(pageToNavigate !=''){
        this.router.navigate([pageToNavigate],{state: item});
      }
  }

  myWallet(){
    this.router.navigate(['tabs/tab4']);
  }
  fabitem : {card_type: string};
  myJobs(num){

    switch(num){
      case 1:
        this.fabitem  = {card_type:'active'};
        break;

      case 2:
        this.fabitem  = {card_type:'REVIEWJOB'};
        break;

      case 3:
        this.fabitem  = {card_type:'PASTDUEJOB'};
        break;
    }
    this.router.navigate(['jobmanager'],{state: this.fabitem});
  }
  selBlogResult:any;
  getOneBlog(item){
      const bligTmF ={
        sessionid: this.sessionDataProviderService.session,
        blog_id: item.blog_id,
        limit: 1
      };

    this.wrenchService.getBlogData(bligTmF).subscribe(
        blogResult1 => {
          this.selBlogResult = blogResult1.blogdata[0];
          console.log("selBlogResult RETURN->", this.selBlogResult);
          this.blogDetails(this.selBlogResult );
        }
    );
    return  this.selBlogResult;
  }
  blogDetails(item) {
    var pageToNavigate = 'blogdetail';
    this.router.navigate([pageToNavigate],{state: item} );
  }

allBlog(){
  this.router.navigate(['allblog']);
}

  onNotification() {
    this.router.navigate(['notification']);
  }

  onTopMentors() {
    this.router.navigate(['top-mentors']);
  }

  onBookmark() {
    this.router.navigate(['bookmark']);
  }

  onPopular() {
    this.router.navigate(['popular']);
  }

  onSearch() {
    this.router.navigate(['search']);
  }


}
