//
//  DrawView.m
//  WindowmakerMeasureCPA
//
//  Created by Admin on 22/01/1939 Saka.
//
//

#import "DrawView.h"

@implementation DrawView

// -------------------------------------------------------------------------------
// Used for drawing the grids ontop of the view port
// -------------------------------------------------------------------------------
- (void)drawRect:(CGRect)rect
{
    
    CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
    CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
    
    CGFloat maxWidth=screenWidth;
    CGFloat maxHeight=screenHeight;
    CGFloat srcWidth=2448;
    CGFloat srcHeight=3264;
    
    if ( UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad )
    {/* Device is iPhone */
        srcWidth=2448;
        srcHeight=3264;
    }else{ /* Device is iPad */
        srcWidth=screenWidth;
        srcHeight=screenHeight;
    }
    
    CGFloat ratioWidth=maxWidth / srcWidth;
    CGFloat ratioHeight=maxHeight / srcHeight;
    
    CGFloat ratio = MIN(ratioWidth, ratioHeight);
    
    //CGFloat finalWidth=srcWidth * ratio;
    CGFloat finalHeight=srcHeight * ratio;

    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 0.5);
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    
    // ---------------------------
    // Drawing column lines
    // ---------------------------
    int numberOfColumns=2;
    int numberOfRows=2;
    // calculate column width
    CGFloat columnWidth = self.frame.size.width / (numberOfColumns + 1.0);
    
    for(int i = 1; i <= numberOfColumns; i++)
    {
        CGPoint startPoint;
        CGPoint endPoint;
        
        startPoint.x = columnWidth * i;
        
        if ( UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad )
        {/* Device is iPhone */
            startPoint.y = 50.0f;
        }else{ /* Device is iPad */
            startPoint.y = 0.0f;
        }
    
        endPoint.x = startPoint.x;
        
        if ( UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad )
        {/* Device is iPhone */
            endPoint.y = finalHeight+50.0f;
        }else{ /* Device is iPad */
            endPoint.y = finalHeight;
        }
        
        
        
        CGContextMoveToPoint(context, startPoint.x, startPoint.y);
        CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
        CGContextStrokePath(context);
    }
    
    // ---------------------------
    // Drawing row lines
    // ---------------------------
    
    // calclulate row height
    CGFloat rowHeight = (finalHeight+50.0f) / (numberOfRows + 1.0);
    
    for(int j = 1; j <= numberOfRows; j++)
    {
        CGPoint startPoint;
        CGPoint endPoint;
        
        startPoint.x = 0.0f;
        startPoint.y = rowHeight * j;
        
        endPoint.x = self.frame.size.width;
        endPoint.y = startPoint.y;
        
        CGContextMoveToPoint(context, startPoint.x, startPoint.y);
        CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
        CGContextStrokePath(context);
    }
    
    
    
}



@end
