import { Component, OnInit }  from '@angular/core'

import { Cookie }             from 'ng2-cookies/ng2-cookies'
import { isPlatformBrowser } 	from '@angular/common'

import { StudioService }      from '../../../services/svc.studios'



@Component({
  moduleId: module.id+'',
  selector: 'geo-bar',
  templateUrl: 'tpl.geo-bar.pug',
  styleUrls: ['./sty.geo-bar.scss']
})
export class GeoBarComponent {

  stores: any;

  storeSelected:any = false;
  dropdownOpen = false;

  canUnset: boolean = true;

  scope: any;

  constructor(
    private studioService: StudioService
  ) {
    // Listen to observer from stores
    /*
    studioService.state$.subscribe((response) => {
      // SSR Protection

      if(isPlatformBrowser && !!this.stores) {

        // Check if studio is currently set
        if(Cookie.get('studio') !== response) {
          for(let studio of this.stores) {
            if(response === studio.settings.urlkey) {
              this.storeSelected = studio.settings
              //Cookie.set('studio', studio.settings.urlkey)
            }
          }
        }
      }
    }) */
  }

  ngOnInit() {
    
    this.studioService.getStudios().then(response => {
      this.stores = response.blocks
      
      /*
      // Checks if in browser for access to Document Element and stop loading if stores is undefined
      if(isPlatformBrowser && !!this.stores) {
        if(Cookie.get('studio')) {

          // Iterate studios if cookie is set
          for(let studio of this.stores) {

            // Sets current selection to selection from cookie
            if(studio.settings.urlkey === Cookie.get('studio')) {
              //this.storeSelected = studio.settings
            }
          }
        }
      }
      */
    }) 
  }

  // Triggered on click to save location in cookie
  saveLocation(location) {

    // updates selected store with URLkey
    //this.studioService.updateStore(location.urlkey)
  }

  unset() {
    Cookie.delete('studio')
  }

}