/* Start ----------------------------------------------------- strings.js*/

// ==========================================================================
// ContactsTool English Strings
// ==========================================================================

// Place strings you want to localize here.  In your app, use the key and
// localize it using "key string".loc().  HINT: For your key names, use the
// english string with an underscore in front.  This way you can still see
// how your UI will look and you'll notice right away when something needs a
// localized string added to this file!
//
Object.extend(String.English,{
  // "_String Key": "Localized String"
}) ;


/* End ------------------------------------------------------- strings.js*/

/* Start ----------------------------------------------------- core.js*/

// ==========================================================================
// ContactsTool
// ==========================================================================

ContactsTool = SC.Object.create(SC.Statechart, {

  // This will create the server for your application.  Add any namespaces
  // your model objects are defined in to the prefix array.
  server: SC.Server.create({ prefix: ['ContactsTool'] }),

  // When you are in development mode, this array will be populated with
  // any fixtures you create for testing and loaded automatically in your
  // main method.  When in production, this will be an empty array.
  FIXTURES: [],
  
  //the container on body.js is bound to this property.  it contains the path to the active view
  mainView: '',
  loadingButtons: '',
  
  
  //States
  goStateA1: function() {
   //login screen
   this.set('mainView', 'ContactsTool.loginPage.mainView');
  },
  
  goStateA2: function() {
   //authentication
   //TODO: call the server
   //for now fake a sucessful call
   this.set('mainView','ContactsTool.loadingPage.mainView');
   this.set('loadingButtons', 'ContactsTool.loadingPage.authenticatedButtons');
   //this.authenticated();
  },
  
  goStateA3: function() {
    this.set('mainView','ContactsTool.loadingPage.mainView');
    this.set('loadingButtons', 'ContactsTool.loadingPage.loadingContactsButtons');
   //load contacts
   var records = this.Contact.collection();
   this.masterController.set('content', records);
   records.refresh();
   //this.successfulLoad();
  },
  
  goStateA4: function() {
   //contatcs loaded
   this.set('mainView','ContactsTool.contactsPage.mainView');
  },
  
  goStateA5: function() {
   //loading contacts wizard
  },
  
  goStateA6: function() {
   //failure

  },
  
  goStateA7: function() {
   //switch state
  },
  
  goStateC1: function() {
   //retry wait
   this.set('mainView','ContactsTool.errorPage.retryWait');
  },
  
  goStateC2: function() {
   //wait server response
   this.set('mainView','ContactsTool.errorPage.waitServerResponse');
  },
  
  goStateC5: function() {
   //Fatal server error
   this.set('mainView','ContactsTool.errorPage.fatalError');
  },
  
  
  
  //core actions
  submitLogin: function() {
    var handled = NO;
    
    switch(this.state.a){
        case 1:
          this.goState('a',2);
          handled = YES;
          break;
    }
    if(!handled) console.log('ContactsTool#submitLogin Action not handled in state %@[%@]'.fmt('a',this.state.a));
  },
  
  authenticated: function() {
    var handled = NO;
    
    switch(this.state.a){
        case 2:
          this.goState('a', 3);
          handled = YES;
          break;
    }
    if(!handled) console.log('ContactsTool#authentiated Action not handled in state %@[%@]'.fmt('a',this.state.a));
  },
  
  logout: function() {
    var handled = NO;
    
    switch(this.state.a){
        case 4:
        case 6:
          this.goState('a', 1);
          handled = YES;
          break;
    }
    if(!handled) console.log('CotnactsTool#logout Action not handled in state %@[%@]'.fmt('a',this.state.a));
  },
  
  successfulLoad: function() {
    var handled = NO;
    
    switch(this.state.a){
        case 3:
          this.goState('a', 4);
          handled = YES;
          break;
    }
    if(!handled) console.log('ContactsTool#successfulLoad Action not handled in state %@[%@]'.fmt('a',this.state.a));
  },
  
  loadContacts: function() {
    var handled = NO;
    
    switch(this.state.a){
        case 4:
          this.goState('a', 5);
          this.goState('b', 1);
          handled = YES;
          break;
    }
    if(!handled) console.log('ContactsTool#loadContacts Action not handled in state %@[%@]'.fmt('a',this.state.a));
  },
  
  faildLogin: function() {
    var handled = NO;
    
    switch(this.state.a){
        case 2:
          this.goState('a',1);
          handled = YES;
          break;
    }
    if(!handled) console.log('ContactsTool#faildLogin Action not handled in state %@[%@]'.fmt('a',this.state.a));
  },
  
  retryError: function() {
    var handled = NO;
    
    switch(this.state.a){
        case 3:
        case 5:
          this.goState('a', 6);
          this.goState('c', 1);
          handled = YES;
          break;
        case 6:
          switch(this.state.c){
            case 2:
              this.goState('c',1);
              handled = YES;
              break;
          }
          break;
          
    }
    if(!handled) console.log('ContactsTool#retryError Action not handled in state %@[%@]'.fmt('a',this.state.a));
  },
  
  fatalError: function() {
    var handled = NO;
    
    switch(this.state.a){
        case 3:
        case 5:
          this.goState('a', 6);
          this.goState('c', 5);
          handled = YES;
          break;
        case 6:
          switch(this.state.c){
            case 2:
              this.goState('c', 5);
              handled= YES;
              break;
          }
          
          //handled = YES;
          break;
    }
    if(!handled) console.log('ContactsTool#fatalError Action not handled in state %@[%@]'.fmt('a',this.state.a));
  },
  
  retryCall: function() {
    var handled = NO;
    
    switch(this.state.c){
        case 1:
          this.goState('c', 2);
          handled = YES;
          break;
    }
    if(!handled) console.log('ContactsTool#retryCall Action not handled in state %@[%@]'.fmt('c',this.state.c));
  },
  
  timeout: function() {
    var handled = NO;
    
    switch(this.state.c){
        case 2:
          this.goState('c', 1);
          handled = YES;
          break;
    }
    if(!handled) console.log('ContactsTool#timeout Action not handled in state %@[%@]'.fmt('c',this.state.c));
  },
  
  failedLogin: function() {
    var handled = NO;
    
    switch(this.state.a){
        case 2:
          this.goState('a', 1);
          handled = YES;
          break;
    }
    if(!handled) console.log('ContactsTool#failedLogin Action not handled in state %@[%@]'.fmt('a',this.state.a));
  },
  
  retrySuccess: function() {
    var handled = NO;
    
    switch(this.state.a){
        case 6:
          this.goState('a', 4);
          handled = YES;
          break;
    }
    if(!handled) console.log('ContactsTool#retrySuccess Action not handled in state %@[%@]'.fmt('a',this.state.a));
  }

}) ;


/* End ------------------------------------------------------- core.js*/

/* Start ----------------------------------------------------- error.js*/

// ==========================================================================
// ContactsTool.errorPage
// ==========================================================================

/** @static

  @extends SC.Page
  @author Mike Ball

*/
ContactsTool.errorPage = SC.Page.create(
/** @scope ContactsTool.loadingPage.prototype */ {
  // needsDesigner: YES,
  retryWait: SC.View.design({
    layout: { top: 0, right: 0, bottom: 0, left: 0 },
    
    childViews: [
      SC.ImageView.design({
        layout: { centerX: 0, centerY: 0, height: 48, width: 48 },
        status: 'loaded',
        value: '/sc-apps/sc-statechart/contacts-tool/en/desktop/f5ce028582639dd4530406bb4520d77edcff302f/images/indicator.gif'
      }),
      SC.LabelView.design({
        layout: { centerX: 5,centerY: 52,height: 40,width: 110 },
        escapeHTML: NO,
        value: "<h2>Retry Wait</h2>"
      }),
      SC.ButtonView.design({
        layout: {centerX: -64,centerY: 90,width: 86,height: 21},
        target: 'ContactsTool',
        action: 'retryCall',
        title: 'retryCall'
      }),
      SC.ButtonView.design({
        layout: {centerX: 85,centerY: 89,width: 86,height: 21},
        target: 'ContactsTool',
        action: 'logout',
        title: 'logout'
      })
    ]
  }),
  
  waitServerResponse: SC.View.design({
    layout: { top: 0, right: 0, bottom: 0, left: 0 },
    
    childViews: [
      SC.ImageView.design({
        layout: { centerX: 0, centerY: 0, height: 48, width: 48 },
        status: 'loaded',
        value: '/sc-apps/sc-statechart/contacts-tool/en/desktop/f5ce028582639dd4530406bb4520d77edcff302f/images/indicator.gif'
      }),
      SC.LabelView.design({
        layout: { centerX: 50, centerY: 50, height: 40, width: 400 },
        escapeHTML: NO,
        value: "<h2>Waiting for server response</h2>"
      }),
      SC.ButtonView.design({
        layout: {centerX: 0, centerY: 90, width: 86,height: 21},
        target: 'ContactsTool',
        action: 'retryError',
        title: 'retryError'
      }),
      SC.ButtonView.design({
        layout: {centerX: 200, centerY: 90, width: 86,height: 21},
        target: 'ContactsTool',
        action: 'retrySuccess',
        title: 'retrySuccess'
      }),
      SC.ButtonView.design({
        layout: {centerX: 300, centerY: 90, width: 86,height: 21},
        target: 'ContactsTool',
        action: 'timeout',
        title: 'timeout'
      }),
      SC.ButtonView.design({
        layout: {centerX: 400, centerY: 90, width: 86,height: 21},
        target: 'ContactsTool',
        action: 'fatalError',
        title: 'fatalError'
      }),
      SC.ButtonView.design({
        layout: {centerX: 500, centerY: 90, width: 86,height: 21},
        target: 'ContactsTool',
        action: 'logout',
        title: 'logout'
      })
    ]
  }),
  
  fatalError: SC.View.design({
    layout: { top: 0, right: 0, bottom: 0, left: 0 },
    
    childViews: [
      SC.LabelView.design({
        layout: { centerX: 50, centerY: 50, height: 40, width: 400 },
        escapeHTML: NO,
        value: "<h2>Server Error</h2>"
      }),
      SC.ButtonView.design({
        layout: {centerX: -9,centerY: 112,width: 86,height: 21},
        target: 'ContactsTool',
        action: 'logout',
        title: 'logout'
      })
    ]
  })
  
});


/* End ------------------------------------------------------- error.js*/

/* Start ----------------------------------------------------- controllers/detail.js*/

// ==========================================================================
// ContactsTool.DetailController
// ==========================================================================

require('core');

/** @class

  ported from sproutcore sample app contacts
  @extends SC.Object
  @author    AuthorName
  @version 0.1
  @static
*/
ContactsTool.detailController = SC.ObjectController.create(
/** @scope ContactsTool.detailController */ {

  // TODO: Add your own code here.
  contentBinding: 'ContactsTool.masterController.selection',

  commitChangesImmediately: false

}) ;


/* End ------------------------------------------------------- controllers/detail.js*/

/* Start ----------------------------------------------------- contacts.js*/

// ==========================================================================
// ContactsTool
// ==========================================================================
 
require('core');
 
/** @class
 
 
@extends SC.Page
@author AuthorName

*/
ContactsTool.contactsPage = SC.Page.create( // SC.Statechart,
/** @scope ContactsTool.contactsPage.prototype */ {
  
  // needsDesigner: YES,
  mainView: SC.View.design({
    layout: {left: 0, top:0, bottom:0, right: 0},
    childViews:[
      SC.ScrollView.design({
        layout: { left: 0, width: 200, top: 0, bottom: 0 },
        contentView: SC.ListView.design({
          contentValueKey: 'fullName',
          contentBinding: 'ContactsTool.masterController.arrangedObjects',
          selectionBinding: 'ContactsTool.masterController.selection'
        })
      }),
      SC.LabelView.design({
        layout: {centerX: -126,top: 101,width: 73,height: 23},
        value: 'First Name'
      }),
      SC.TextFieldView.design({
        layout: {centerX: -39,top: 121,width: 254,height: 24},
        valueBinding: 'ContactsTool.detailController.firstName'
      }),
      SC.LabelView.design({
        layout: {centerX: -124,top: 171,width: 73,height: 23},
        value: 'Last Name'
      }),
      SC.TextFieldView.design({
        layout: {centerX: -37,top: 189,width: 254,height: 24},
        valueBinding: 'ContactsTool.detailController.lastName'
      }),
      SC.ButtonView.design({
        layout: {centerX: 45,top: 240,width: 86,height: 21},
        target: 'ContactsTool.detailController',
        action: 'commitChanges',
        isEnabledBinding: 'ContactsTool.detailController.hasChanges',
        title: 'Save'
      }),
      SC.ButtonView.design({
        layout: {centerX: -62,top: 241,width: 86,height: 21},
        target: 'ContactsTool.detailController',
        action: 'discardChanges',
        isEnabledBinding: 'ContactsTool.detailController.hasChanges',
        title: 'Cancel'
      }),
      SC.ButtonView.design({
        layout: {left: 220,bottom: 20, width: 86,height: 21},
        target: 'ContactsTool',
        action: 'loadContacts',
        title: 'Upload Wizard'
      }),
      SC.ButtonView.design({
        layout: {left: 320,bottom: 20,width: 86,height: 21},
        target: 'ContactsTool',
        action: 'logout',
        title: 'Logout'
      })
    ] 
  })
 
});

/* End ------------------------------------------------------- contacts.js*/

/* Start ----------------------------------------------------- controllers/master.js*/

// ==========================================================================
// ContactsTool.MasterController
// ==========================================================================

require('core');

/** @class

  (Document Your View Here)
  
  Ported from sproutcore sample app
  
  @extends SC.Object
  @author    AuthorName
  @version 0.1
  @static
*/
ContactsTool.masterController = SC.CollectionController.create(
/** @scope ContactsTool.masterController */ {

  // TODO: Add your own code here.
  allowsEmptySelection: false,
  allowsMultipleSelection: false

}) ;


/* End ------------------------------------------------------- controllers/master.js*/

/* Start ----------------------------------------------------- models/contact.js*/

// ==========================================================================
// ContactsTool.Contact
// ==========================================================================

require('core');

/** @class

  (Document your class here)

  @extends SC.Record
  @author    AuthorName
  @version 0.1
*/
ContactsTool.Contact = SC.Record.extend(
/** @scope ContactsTool.Contact.prototype */ {

  // TODO: Add your own code here.
  fullName: function() {
    return [this.get('firstName'), this.get('lastName')].compact().join(' ');
  }.property('firstName', 'lastName')

}) ;


/* End ------------------------------------------------------- models/contact.js*/

/* Start ----------------------------------------------------- body.js*/

// ==========================================================================
// ContactsTool.bodyPage
// ==========================================================================

/** @static
    
  @extends SC.Page
  @author Mike Ball
*/
ContactsTool.bodyPage = SC.Page.create( 
/** @scope ContactsTool.bodyPage.prototype */ {
  needsDesigner: YES,
  mainPane: SC.MainPane.design({
    childViews: [
      SC.ContainerView.extend({
        layout: {top: 0, left: 0, bottom: 0, right: 0},
        nowShowingBinding: 'ContactsTool.mainView'
      })
    ]
  })
});


/* End ------------------------------------------------------- body.js*/

/* Start ----------------------------------------------------- main.js*/

// ==========================================================================
// ContactsTool
// ==========================================================================

// This is the function that will start your app running.  The default
// implementation will load any fixtures you have created then instantiate
// your controllers and awake the elements on your page.
//
// As you develop your application you will probably want to override this.
// See comments for some pointers on what to do next.
//
function main() {


  ContactsTool.server.preload(ContactsTool.FIXTURES) ;

  ContactsTool.getPath('bodyPage.mainPane').append();
  
  ContactsTool.goState('a',1);
} ;


/* End ------------------------------------------------------- main.js*/

/* Start ----------------------------------------------------- loading.js*/

// ==========================================================================
// ContactsTool.loadingPage
// ==========================================================================

/** @static

  @extends SC.Page
  @author Mike Ball

*/
ContactsTool.loadingPage = SC.Page.create(
/** @scope ContactsTool.loadingPage.prototype */ {
  
  mainView: SC.View.design({
    layout: { top: 0, right: 0, bottom: 0, left: 0 },
    
    childViews: [
      SC.ImageView.design({
        layout: { centerX: 0, centerY: 0, height: 48, width: 48 },
        status: 'loaded',
        value: '/sc-apps/sc-statechart/contacts-tool/en/desktop/f5ce028582639dd4530406bb4520d77edcff302f/images/indicator.gif'
      }),
      SC.LabelView.design({
        layout: { centerX: 50, centerY: 50, height: 40, width: 400 },
        escapeHTML: NO,
        value: "<h2>Loading</h2>"
      }),
      SC.ContainerView.design({
        layout: { centerX: 50, centerY: 200, height: 40, width: 400 },
        nowShowingBinding: 'ContactsTool.loadingButtons'
      })
    ]
  }),
  
  authenticatedButtons:  SC.View.design({
    layout: {top: 0, left: 0, right: 0, bottom: 0},
    
    childViews:[
      SC.ButtonView.design({
        layout: {top: 0, left: 0, width: 86,height: 21},
        target: 'ContactsTool',
        action: 'authenticated',
        title: 'authenticated'
      }),
      SC.ButtonView.design({
        layout: {top: 0, right: 0,width: 86,height: 21},
        target: 'ContactsTool',
        action: 'failedLogin',
        title: 'failedLogin'
      })
    ]
  }),
  
  loadingContactsButtons: SC.View.design({
    layout: {top: 0, left: 0, right: 0, bottom: 0},
    
    childViews:[
      SC.ButtonView.design({
        layout: {top: 0, left: 0, width: 100,height: 21},
        target: 'ContactsTool',
        action: 'successfulLoad',
        title: 'successfulLoad'
      }),
      
      SC.ButtonView.design({
        layout: {top: 0, centerX: 0, width: 86,height: 21},
        target: 'ContactsTool',
        action: 'retryError',
        title: 'retryError'
      }),
      
      SC.ButtonView.design({
        layout: {top: 0, right: 0,width: 86,height: 21},
        target: 'ContactsTool',
        action: 'fatalError',
        title: 'fatalError'
      })
    ]
  })
  
});


/* End ------------------------------------------------------- loading.js*/

/* Start ----------------------------------------------------- login.js*/

// ==========================================================================
// ContactsTool
// ==========================================================================
 
require('core');
 
/** @class
 
 
@extends SC.Page
@author AuthorName

*/
ContactsTool.loginPage = SC.Page.create( // SC.Statechart,
/** @scope ContactsTool.loginPage.prototype */ {
  
  mainView: SC.View.design({
 
    childViews: [
      SC.LabelView.design({
        escapeHTML: NO,
        styleClass: 'welcome-tab',
        value: "<h1>Contacts Tool (with statecharts)</h1>"
      }),
      
      SC.ButtonView.design({
        layout: { height: 20, width: 150, centerX: 0, centerY: 0 },
        title: 'Login',
        target: ContactsTool,
        action: 'submitLogin'
      })
    ]
    
  })
 
});

/* End ------------------------------------------------------- login.js*/

/* Start ----------------------------------------------------- fixtures/contact.js*/

// ==========================================================================
// ContactsTool.Contact Fixtures
// ==========================================================================

require('core') ;

ContactsTool.FIXTURES = ContactsTool.FIXTURES.concat([

  // TODO: Add your data fixtures here.
  // All fixture records must have a unique guid and a type matching the
  // name of your contact.  See the example below.

  { guid: 1,
    type: 'Contact',
    firstName: "Michael",
    lastName: "Scott"
  },

  { guid: 2,
    type: 'Contact',
    firstName: "Dwight",
    lastName: "Schrute"
  },

  { guid: 3,
    type: 'Contact',
    firstName: "Jim",
    lastName: "Halpert"
  },

  { guid: 4,
    type: 'Contact',
    firstName: "Pam",
    lastName: "Beesly"
  },

  { guid: 5,
    type: 'Contact',
    firstName: "Ryan",
    lastName: "Howard"
  }

]);


/* End ------------------------------------------------------- fixtures/contact.js*/

