/**
 *Fills the code queue based on the provided Product entity and adds code
 * export data to the Data table
 * 
 * @global WindowsAzureQueue $queue
 * @global type $logger
 * @global WindowsAzureTable $table
 * @global type $timer
 * @param Product $p 
 */
function fill_new_product_queue($p)
{
    global $queue, $table, $timer;
    $i = $p->NumProducts;
    $times = array();
    // holds the times codes have been created for
    $export = array();
    // holds codes to be exported via csv
    $timer->mark('start_build_prod_codes');
    while ($i > 0) {
        //echo "\nLoop: $i/" . $p->NumProducts;
        $t = rand(strtotime($p->StartDate), strtotime($p->EndDate));
        /*
         * Need to check these failing conditions.
         * If they exists create a new code
         * 
         * $t = new time
         * $times = list of used times
         * 
         * $t already in $times
         * not in $valid_days
         * 
         */
        // echo "\nI am in the array: "; var_dump(in_array(date('D', $t));
        if (in_array($t, $times) || !in_array(date('D', $t), $p->ValidDays) || date('G', $t) < 8 || date('G', $t) > 15) {
            //echo "\ncontinueing";
            continue;
        }
        /*
         * Otherwise no conflict has been found. Free to use this time
         * Add it to the $times array
         */
        $times[] = $t;
        /*
         * Now that we have a time we can add the new redemption code to the db.
         * NOTE that sometimes this fails to authenticate. No idea why so I am
         * doing some very hacky "trapping" of this dumbness
         */
        $code = generate_code($p->StartDate, $p->EndDate);
        $a = array('PartitionKey' => $p->getPartitionKey(), 'RowKey' => $p->getRowKey(), 'Code' => $code, 'Valid' => $t, 'StartDate' => $p->StartDate, 'EndDate' => $p->EndDate, 'ValidDays' => $p->ValidDays);
        $try = 1;
        // Watch how many tries it has taken to insert this code to the queue
        do {
            try {
                $again = false;
                $queue->putMessage('code', serialize($a));
            } catch (Exception $e) {
                echo "\nFailed to insert product code to queue on try {$try}";
                $try++;
                $again = true;
            }
        } while ($again);
        /*
         * Setup export information so codes can easily be given to vendor
         */
        $export[] = array('ProductId' => $p->getRowKey(), 'Valid' => $t, 'Code' => $code, 'StartDate' => $p->StartDate, 'EndDate' => $p->EndDate, 'ValidDays' => $p->ValidDays);
        /*
         * Reduce the counter or DIE!!MUAUAHAHA
         */
        $i--;
    }
    $timer->mark('stop_build_prod_codes');
    $timer->mark('start_insert_export');
    /*
     * Store the export data in the Data table in the ProductExport partition
     */
    $try = 1;
    // Watch how many tries it has taken to insert this code to the queue
    do {
        $again = false;
        try {
            $d = new Data('ProductExport', $p->getRowKey());
            $d->Value = serialize($export);
            $table->insertEntity('Data', $d);
            echo "\n --- Inserted code export data into table";
        } catch (Exception $e) {
            echo "\nFailed to insert code export to table on try {$try}";
            $try++;
            $again = true;
        }
    } while ($again);
    $timer->mark('stop_insert_export');
}