import Ember from 'ember';
import $ from 'jquery';

export default Ember.Component.extend({
  indexedDbPromised: Ember.inject.service('indexed-db'),
  tagName: 'li',
  classNames: ['col s12 route-single card'],

  actions: {
    showDetails(e){
      $(e.element).find('.route-details').toggleClass('show-details');
      $('.tooltipped').tooltip({
        delay: 20
      });
    },

    addToFavorites(e){

      // TODO move prototype to global, refactor long string
      if (!Array.prototype.last){
          Array.prototype.last = function(){
              return this[this.length - 1];
          };
      }
      this.get('indexedDbPromised').saveToDb({
        $dbName: 'transportme-favorites',
        $dbStore: 'favorites',
        $key: `${JSON.parse(JSON.stringify(e.route_parts[0].from_point_name))}->${JSON.parse(JSON.stringify(e.route_parts.last().to_point_name))}`,
        $value: JSON.parse(JSON.stringify(e))
      }).then(()=>{
        console.log('added to favorites');
      });
    }
  }

});

@@ -398,16 +398,20 @@
 orites(e
+vent
 )%7B%0A%0A    
@@ -416,194 +416,509 @@
     
-// TODO move prototype to global, refactor long string%0A      if (!Array.prototype.last)%7B%0A          Array.prototype.last = function()%7B%0A              return this%5Bthis.length - 1%5D
+return new Promise((resolve, reject)=%3E%7B%0A        if(!event) %7B reject(); %7D%0A%0A        const el = $(this.get('element')).find('.set-favorite');%0A        el.toggleClass('favorite-added');%0A        resolve(el.hasClass('favorite-added'));%0A%0A      %7D).then((isInDB)=%3E%7B%0A%0A        const jorneyId = %60$%7BJSON.parse(JSON.stringify(event.route_parts%5B0%5D.departure_time))%7D-%3E$%7BJSON.parse(JSON.stringify(event.route_parts%5B0%5D.from_point_name))%7D-%3E$%7BJSON.parse(JSON.stringify(event.route_parts.last().to_point_name))%7D%60
 ;%0A
+%0A
         
   %7D;
@@ -917,26 +917,37 @@
     
-  %7D;
+if(!isInDB)%7B
 %0A      
-%7D%0A
     
- 
+return
  thi
@@ -977,19 +977,33 @@
 d').
-saveToDb
+removeById
 (%7B%0A
+            
     
@@ -1032,32 +1032,44 @@
 tme-favorites',%0A
+            
         $dbStore
@@ -1095,148 +1095,363 @@
     
-$key: %60$%7BJSON.parse(JSON.stringify(e.route_parts%5B0%5D.from_point_name))%7D-%3E$%7BJSON.parse(JSON.stringify(e.route_parts.last().to_point_name))%7D%60,%0A
+            $id: jorneyId%0A                  %7D).then(()=%3E%7B%0A                    console.log(%60$%7BjorneyId%7D removed from favorites%60);%0A                  %7D);%0A        %7D%0A%0A        return this.get('indexedDbPromised').saveToDb(%7B%0A                  $dbName: 'transportme-favorites',%0A                  $dbStore: 'favorites',%0A                  $key: jorneyId,%0A          
     
@@ -1493,11 +1493,25 @@
 fy(e
-))%0A
+vent))%0A          
     
@@ -1518,32 +1518,42 @@
   %7D).then(()=%3E%7B%0A
+          
         console.
@@ -1556,17 +1556,96 @@
 ole.log(
-'
+%60$%7BjorneyId%7D added to favorites%60);%0A                  Materialize.toast('Journey 
 added to
@@ -1655,16 +1655,22 @@
 vorites'
+, 1000
 );%0A     
@@ -1674,21 +1674,72 @@
     
-%7D);%0A    %7D%0A  %7D
+          %7D);%0A      %7D);%0A    %7D// add to favorites%0A%0A  %7D,//Actions%0A
 %0A%0A%7D)

Add or remove from favorites action