import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { Router } from '@angular/router'


@Injectable({
  providedIn: 'root'
})
export class MermsSessionService {

  constructor(
    public http: HttpClient,
    private router: Router

  ) { }

  list_bank_count: number = 0

  currentFooterPage = 'HOME'
  top_card = '' // "https://www.float.sg/float/static_top.png";
  firstname = ''
  lastname = ''
  session = 'YESADUMMUFORNOW'
  balance = 0
  username = ''
  last_login = ''
  member_id = 0
  added = ''
  account_added = ''
  public country = ''
  email = ''
  city = ''
  fb_id = ''
  loc = ''
  news = ''
  id = 0
  phone = ''
  post_jobs = ''
  profile_pic = ''
  refer = ''
  state = ''
  status = 0
  street1 = ''
  zipcode = ''
  account_token = ''
  points = 0
  min_budget = 0
  max_budget = 0
  test = 1 // 0;
  count_acct = 0
  count_email = 0
  testAccount = 1 // Emmergency fix, remove once bookig flow is fixed
  alert_email = true
  alert_notification = true
  // settings
  FEED_BOOKING_SHOW = 100
  max_deals_cards = 5
  start_city = '' // "Powder Springs"
  start_country = '' //  "US"
  start_state = '' // "GA"

  public player_id = ''
  public access_token = ''
  public refresh_token = ''

  public gps_city_location = '....'
  public gps_fulladdress_location = ''
  public gps_latitude
  public gps_longitude
  public dash_data_refresh_seconds = 15000
  public gps_country_code = ''
  public data_collection_mode = 1

  public prompt_background_gps = true
  session_contructed = false

  public personality_name = ''

  public lastSessionCheck = null
  public readonly applicationStarted = new Date().getTime()

  ConstructGlobalSessionData(loginResult: any) {
    console.log('FloatSessionProvider::ConstructGlobalSessionData()')
    this.session_contructed = false

    try {
      console.log('Login result session:', loginResult)
      // alert(loginResult.firstname);
      this.firstname = loginResult.firstname
      this.lastname = loginResult.lastname

      if (
        loginResult.hasOwnProperty('sessionid') &&
        loginResult.sessionid !== ''
      ) {
        this.session = loginResult.sessionid
        loginResult.session = loginResult.sessionid
        delete loginResult.sessionid
      } else {
        this.session = loginResult.session
        if (loginResult.hasOwnProperty('sessionid')) {
          delete loginResult.sessionid
        }
      }

      this.username = loginResult.username
      this.last_login = loginResult.last_login
      this.member_id = loginResult.member_id
      this.balance = loginResult.balance
      this.added = loginResult.added
      this.account_added = loginResult.account_added
      this.city = loginResult.city
      this.country = loginResult.country
      if (this.country === '') {
        // this.country = 'SG';
      }
      this.email = loginResult.email
      this.fb_id = loginResult.fb_id
      this.id = loginResult.id
      this.loc = loginResult.loc
      this.news = loginResult.news
      this.phone = loginResult.phone
      this.post_jobs = loginResult.post_jobs
      this.profile_pic = loginResult.profile_pic
      this.refer = loginResult.refer
      this.state = loginResult.state
      this.status = loginResult.status
      this.street1 = loginResult.street1
      this.zipcode = loginResult.zipcode
      this.test = 1 // loginResult.test;
      this.points = loginResult.points
      this.min_budget = loginResult.min_budget
      this.max_budget = loginResult.max_budget
      this.top_card = loginResult.top_card
      this.count_acct = loginResult.count_acct
      this.count_email = loginResult.count_email
      this.account_token = loginResult.account_token

      this.alert_email = loginResult.alert_email === 1 ? true : false
      this.alert_notification =
        loginResult.alert_notification === 1 ? true : false
      /*
      START city , state or Country is the first detected location of the account - not a profile location 
      */

      if (loginResult && loginResult.hasOwnProperty('max_deals_cards')) {
        this.max_deals_cards = loginResult.max_deals_cards
      }

      if (loginResult && loginResult.hasOwnProperty('start_city')) {
        this.start_city = loginResult.start_city
      }
      if (loginResult && loginResult.hasOwnProperty('start_country')) {
        this.start_country = loginResult.start_country
      }
      if (loginResult && loginResult.hasOwnProperty('start_city')) {
        this.start_state = loginResult.start_state
      }

      // app settings parameters
      this.FEED_BOOKING_SHOW = loginResult.FEED_BOOKING_SHOW
      if (loginResult && loginResult.hasOwnProperty('personality_name')) {
        this.personality_name = loginResult.personality_name
      }

      if (loginResult && loginResult.hasOwnProperty('access_token')) {
        this.access_token = loginResult.access_token
        this.refresh_token = loginResult.refresh_token
      }
      if (loginResult && loginResult.hasOwnProperty('player_id')) {
        this.player_id = loginResult.player_id
      }

      if (loginResult && loginResult.hasOwnProperty('prompt_background_gps')) {
        this.prompt_background_gps = loginResult.prompt_background_gps
      } else {
        this.prompt_background_gps = true // Let's ask
      }

      // alert("Got Here");
      // this.pushNotificationProvider.tagUser(this.session);
      this.session_contructed = true

      // console.log(loginResult);
      console.log(loginResult)
      console.log('LOGIN STORAGE COMPLETED')
    } catch (error) {
      console.log('LOGIN STORAGE ERROR -> ' + error)
      this.session_contructed = false
    }

    return this.session_contructed
  }

  updateSession(cleanUp) {
    console.log('FloatSessionProvider::updateSession(' + cleanUp + ')')
    let updateResult: any
  }

  

  validateEmail(email) {
    const re = /\S+@\S+\.\S+/;
    return re.test(email);
  }

}
