﻿/*
 * Ext Scheduler v1.0 beta
 * Copyright(c) 2009 Mats Bryntse Consulting
 * mats@ext-scheduler.com
 * http://www.ext-scheduler.com/license.html
 *
 */

/*
 * Include this on your HTML page and use firebug (or any other console application) to execute line below:
 * >
 * > schedulerDiagnostics();
 * > ...
 */ 
function schedulerDiagnostics() {
    var s = Ext.getCmp(Ext.select('.sch-schedulerpanel').elements[0].id),
        es = s.eventStore;
    
    if (!s.store) throw('No store configured');
    if (!es) throw('No event store configured');
    
    
    if (s.autoViews) {
        console.info('Autoviews configured. Grid will reconfigure itself when you load the store using the start/end param values.');
        
        if (!es.lastOptions) {
            throw('Event store has not been loaded.');
        } else if (!es.lastOptions.params[s.startParamName] || 
                   !es.lastOptions.params[s.endParamName]) {
            throw('Store was loaded with incorrect date parameters. The start/end param names must be set on the schedulerpanel as "startParamName" and "endParamName".');
        }
    } else {
        console.info('Autoviews is not configured, the grid must be manually configured."'); 
    }
    
    console.info(s.store.getCount() + ' store records in the resource store'); 
    console.info(es.getCount() + ' event records in the eventStore'); 
    console.info(Ext.select(s.eventSelector).getCount() + ' events present in the DOM'); 
    
    if (es.getCount() > 0) {
        if (!es.getAt(0).get('StartDate') || !(es.getAt(0).get('StartDate') instanceof Date)) {
            throw ('The eventStore reader is misconfigured - The StartDate field is not setup correctly, please investigate');
        }
        
        if (!es.getAt(0).get('EndDate') || !(es.getAt(0).get('EndDate') instanceof Date)) {
            throw('The eventStore reader is misconfigured - The EndDate field is not setup correctly, please investigate');
        }
        
        if (!es.fields.get('Id')) {
            throw('The eventStore reader is misconfigured - The Id field is not present');
        }
        
        if (!es.fields.get('ResourceId')) {
            throw('The eventStore reader is misconfigured - The ResourceId field is not present');
        }
        
        console.info('Records in the event store:');
        es.each(function(r, i) {
            console.log((i + 1) + '. Start:' + r.get('StartDate') + ', End:' + r.get('EndDate'));
        });
    } else {
        throw('Event store has no data.');
    }
    
    if (s.store.getCount() > 0) {
        if (!s.store.fields.get('Id')) {
            throw('The resource store reader is misconfigured - The Id field is not present');
        }
    } else {
        throw('Resource store has no data.');
    }
    
    console.info('Dumping resource store:');
    s.store.each(function(r){ console.log(r.data);});
    
    console.info('Dumping event store:');
    es.each(function(r){ console.log(r.data);});
    
    console.info('Everything seems to be setup ok!');
}