/*global $:true*/

import Ember from 'ember';
import TimeMachine from 'ember-time-machine';
import _ from 'lodash';



export default Ember.Controller.extend({
    queryParams: ['editMode'],
    showNotSavedModal: false,
    published: false,
    saved: false,
    isAdmin: Ember.computed('node', function(){
        return this.get('model.node.currentUserPermissions').includes('admin');
    }),
    saving: Ember.observer('editMode', function(){

        $(document).ready(()=>{
            let firebaseDB = this.store.findRecord('home', this.get('model.guid'))

            if(this.get('editMode')){

                firebaseDB.then((record)=>{ 
                    let pageData = record.get('unpublishedPageData');
                    if(pageData === null){
                        pageData =  record.get('pageData');
                    }
                    this.set('model.theme.content' , JSON.parse(pageData))  

                });

            }else{
                firebaseDB.then((record)=>{ 
                    let pageData = record.get('pageData');
                    this.set('model.theme.content' , JSON.parse(record.get('pageData')))  
                });
            }

        }); 
    }),
    editMode: false,
    actions: {
        scrollToTop(){
            $('body').animate({scrollTop:0}, '500');
        },
        checkSaveState(){
            if(this.get('editMode')){
                this.set('showNotSavedModal' , true)
            }else{
                this.send('toggleEditMode')
            }

        },
        toggleEditMode(shouldSave){

            if(shouldSave){
                this.send('save' , this.get('model.guid') )
            }

            this.toggleProperty('editMode');
            this.set('showNotSavedModal' , false)

        },
        undo(){
            if(this.get('model.theme').get('canUndo')){
                this.get('model.theme').undo();
            }
        },
        redo(){
            if(this.get('model.theme').get('canRedo')) {
                this.get('model.theme').redo();
            }
        },
        publish(){//publish will take unplblished data and put it in to page data on firebase 
            this.set('published' , true)
            this.send('save' , this.get('model.guid') )
            this.store.findRecord('home', this.get('model.guid')).then((data)=> {               
                data.set('pageData',  data.get('unpublishedPageData'));
                data.save();
            });
            setTimeout(()=>{
                this.set('published' , false)
            }, 2000);
        },
        save(guid){
            if(!this.get('published')){
                this.set('saved' , true)
            }
            if(this.get('editMode')){
                this.store.findRecord('home', this.get('model.guid')).then((data)=> {
                    data.set('unpublishedPageData', JSON.stringify(this.get('model.theme.content')));
                    data.save();
                });
            }
            setTimeout(()=>{
                this.set('saved' , false)
            }, 2000);
        }
    },
    init(){
        this._super(...arguments);
        $('body').on('click', function(e){
            if($(e.target).parents('.popover').length === 0){
                $('.popover').hide();
            }
        })
    }
});

@@ -250,24 +250,47 @@
 ved: false,%0A
+    savedPageData: %22%22,%0A
     isAdmin:
@@ -464,17 +464,16 @@
 tion()%7B%0A
-%0A
         
@@ -492,24 +492,177 @@
 ready(()=%3E%7B%0A
+            this.set('savedPageData' , this.get('model.theme.content.layers')) //save a version of pagedata so we can see if any changes have been made%0A%0A
             
@@ -1560,32 +1560,247 @@
 t('editMode'))%7B%0A
+                if(_.isEqual( this.get('savedPageData'), this.get('model.theme.content.layers') ))%7B//check to see if changes have been made%0A                    this.send('toggleEditMode')%0A                %7Delse%7B%0A    
                 
@@ -1828,32 +1828,50 @@
 dModal' , true)%0A
+                %7D%0A
             %7Dels
@@ -2522,17 +2522,17 @@
 take unp
-l
+u
 blished 

add save state check to see if any changes have been made