//
//  CEPrintViewController.m
//  DemoCustomXmliOSApi
//
//  Created by sviluppo on 27/03/14.
//  Copyright (c) 2014 CUSTOM. All rights reserved.
//

#import "CEPrintViewController.h"
#import "CESecondViewController.h"
#import "CEFirstViewController.h"
#import <CPTiOSApi/CustomXmlDictionaryItem.h>
#import <CPTiOSApi/CustomXmliOSApi.h>

@interface CEPrintViewController () 

@end

@implementation CEPrintViewController

UIImage * previewImg;
int printReturnValue;
UIAlertView * loadingMsg;

@synthesize imagePreview;

static CEPrintViewController *instance =nil;
+(CEPrintViewController *)getInstance
{
    return instance;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    instance = self;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



- (IBAction)doPreview:(id)sender
{
    

    CustomXmliOSApi * api = nil;
    CESecondViewController *paramController = [CESecondViewController getInstance];
    CEFirstViewController *prjController = [CEFirstViewController getInstance];
    
    if ( prjController != nil)
        api =prjController.customXmliOsApi;
    
    if ( api != nil)
    {
        
        loadingMsg = [CEFirstViewController CreateOverMessage:@"Rendering Preview..."];
        [loadingMsg show];
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^ {
            //backgroung processing
            NSError *error=nil;
            NSArray * paramsArray = nil;
            int ret;
            
            if ( paramController != nil)
                paramsArray = paramController.paramsArray;
            
            if ( paramsArray != nil)
            {
                CustomXmlDictionaryItem * item;
                for(int i=0 ; i<paramsArray.count;i++)
                {
                    item = [paramsArray objectAtIndex:i];
                    ret = [ api SetXmlObjectTag:item->Tag :item->Value error:&error];
                }
            }
            
            ret = [ api SetXmlObjectTag:@"Canvas1.antialiasText" :@"1" error:&error];
            ret = [ api SetXmlObjectTag:@"Canvas1.antialiasShape" :@"1" error:&error];
            
#if 0
            {
                BOOL isBold, isItalic, isBoldItalic;
                int is_retval;
                
                is_retval = [api GetFontProperty:@"Arial" :&isBold :&isItalic :&isBoldItalic error:&error];
                is_retval = 0;
            }
#endif
            
            
            previewImg = [ api PreviewXml:&error];
            
            
            
            dispatch_async( dispatch_get_main_queue(), ^ {
                //update UI
                
                self.imagePreview.image = previewImg;

                [loadingMsg dismissWithClickedButtonIndex:0 animated:YES];
                loadingMsg=nil;
                
            });
            
        });
        
        
        
        
        
        
        
#if 0
        if ( paramController != nil)
        paramsArray = paramController.paramsArray;
        
        if ( paramsArray != nil)
        {
            CustomXmlDictionaryItem * item;
            for(int i=0 ; i<paramsArray.count;i++)
            {
                item = [paramsArray objectAtIndex:i];
                ret = [ api SetXmlObjectTag:item->Tag :item->Value error:&error];
            }
        }

        UIImage * img = [ api PreviewXml:&error];
        self.imagePreview.image = img;
#endif
        
    }
    else
    {
        NSString *strmessage =@"Error\nProject not loaded";
        UIAlertView *alerMsg = [[UIAlertView alloc] initWithTitle:@"" message:strmessage delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alerMsg show];
        alerMsg = nil;
    }

    
    
}


- (IBAction)doPrint:(id)sender
{
    CustomXmliOSApi * api = nil;
    CustomPrinter * printer = nil;
    CESecondViewController *paramController = [CESecondViewController getInstance];
    CEFirstViewController *prjController = [CEFirstViewController getInstance];
    printReturnValue = 0;
    NSString  *aliasText=nil;
    NSString *aliasShape =nil;
    
    if ( prjController != nil)
    {
        api =prjController.customXmliOsApi;
        printer = prjController.currentCustomPrn;
        aliasText = prjController.antialiasText;
        aliasShape = prjController.antialiasShape;
        
    }
    
    
    
    
    
    if ( (api != nil) && (printer !=nil) )
    {
        
        loadingMsg = [CEFirstViewController CreateOverMessage:@"Printing..."];
        [loadingMsg show];
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^ {
            //backgroung processing
            NSError *error=nil;
            NSError *errorprn=nil;
            NSArray * paramsArray = nil;
            int ret;
            //NSString *tmp1, *tmp2;
            
            
            
            ret = [ api SetXmlObjectTag:@"Canvas1.antialiasText" :aliasText error:&error];
            ret = [ api SetXmlObjectTag:@"Canvas1.antialiasShape" :aliasShape error:&error];
            //NSString *tmp1 = [ api GetXmlObjectTag:@"Canvas1.antialiasText" error:&error];
            //NSString *tmp2 = [ api GetXmlObjectTag:@"Canvas1.antialiasShape" error:&error];
            
            if ( paramController != nil)
                paramsArray = paramController.paramsArray;
            
            if ( paramsArray != nil)
            {
                CustomXmlDictionaryItem * item;
                for(int i=0 ; i<paramsArray.count;i++)
                {
                    item = [paramsArray objectAtIndex:i];
                    ret = [ api SetXmlObjectTag:item->Tag :item->Value error:&error];
                }
            }
            
           
            
            error=nil;
            //printReturnValue = [ api PrintXml:printer error:&error];
            
            printReturnValue = [ api PrintXml:printer :0 :IMAGE_SCALE_NONE :0 error:&error];
            [printer Present :50 :&errorprn];
            //[printer Eject :EJ_EJECT :&errorprn];
            
            
            dispatch_async( dispatch_get_main_queue(), ^ {
                //update UI
                
                
                [loadingMsg dismissWithClickedButtonIndex:0 animated:YES];
                loadingMsg=nil;
                
                if (printReturnValue == -1)
                {
                    NSString *strmessage;
                    if ( error != nil)
                    {
                        strmessage =[NSString stringWithFormat:@"Printing error\nError %d Descr %@ AddDescr %@", [error code], [error localizedDescription], [error  localizedFailureReason]];
                    }
                    else
                    {
                        strmessage =@"Printing error";
                    }
                    
                    
                    UIAlertView *alerMsg = [[UIAlertView alloc] initWithTitle:@"" message:strmessage delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                    [alerMsg show];
                    alerMsg = nil;
                }
                
                
            });
            
        });
        

        
    }
    else if (api == nil)
    {
        NSString *strmessage =@"Error\nProject not loaded";
        UIAlertView *alerMsg = [[UIAlertView alloc] initWithTitle:@"" message:strmessage delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alerMsg show];
        alerMsg = nil;
    }
    else if (printer == nil)
    {
        NSString *strmessage =@"Error\nPrinter not selected";
        UIAlertView *alerMsg = [[UIAlertView alloc] initWithTitle:@"" message:strmessage delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alerMsg show];
        alerMsg = nil;
    }
    
}



-(void)resetUI
{
    self.imagePreview.image = nil;
    previewImg=nil;
}




@end
