//
//  UserDefines.h
//  JoywayLib
//
//  Created by Bob Xia on 2019/2/18.
//  Copyright © 2019年 ___JOYWAY___. All rights reserved.
//

#ifndef _cn_joyway_lib_misc_CommonDefine_h_
#define _cn_joyway_lib_misc_CommonDefine_h_

#import <UIKit/UIKit.h>

#define COLOR(r,g,b,a)      ([UIColor colorWithRed:r green:g blue:b alpha:a])

#define  SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width
#define  SCREEN_HEIGHT    [UIScreen mainScreen].bounds.size.height

#define iPhone4s ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO) //是否iPhone6

#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO) //是否iPhone5

#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO) //是否iPhone6


/*-------------------系统版本--------------------*/
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

//计算文字长度宽度
#define SIZE_OF_STRING(string,fontsize,maxWidth) string != nil ?[(string) sizeWithFont:[UIFont systemFontOfSize:(fontsize)] constrainedToSize:CGSizeMake((maxWidth), 20000.0f)] : CGSizeMake(0, 0)

//设置矩形大小
#define YW_rect(x,y,w,h) CGRectMake(x,y,w,h)


//tag dictionary 字段名
#define TAG_MAC         @"TAG_MAC"
#define TAG_NAME        @"TAG_NAME"
#define LATITUDE        @"LATITUDE"
#define LONGITUDE       @"LONGITUDE"
#define ADDRESS         @"ADDRESS"
#define LOCATED_TIME    @"LOCATED_TIME"
#define RSSI            @"RSSI"



#define FEET_PER_METER (3.2808399200439453)


//必须添加CG_INLINE内连
CG_INLINE double meterToFeet(double meter)
{
    return meter * FEET_PER_METER;
}

CG_INLINE double feetToMeter(double feet)
{
    return feet / FEET_PER_METER;
}


//----------------------------------------------------------------------/
/*
 * 通知的便捷方法
 */
CG_INLINE void ADD_NOTIFICATION(NSString *name, id target, SEL action, id object)
{
    [[NSNotificationCenter defaultCenter] addObserver:target selector:action name:name object:object];
}

CG_INLINE void REMOVE_NOTIFICATION(NSString *name, id target, id object)
{
    [[NSNotificationCenter defaultCenter] removeObserver:target name:name object:object];
}

CG_INLINE void POST_NOTIFICATION(NSString *name, id object, NSDictionary *userInfo)
{
    [[NSNotificationCenter defaultCenter] postNotificationName:name object:object userInfo:userInfo];
}


//16进制颜色(html颜色值)字符串转为UIColor
CG_INLINE UIColor * hexStringToColor(NSString *stringToConvert)
{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    
    if ([cString length] < 6) return [UIColor blackColor];
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor blackColor];
    
    // Separate into r, g, b substrings
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

//
#define CURRENT_WINDOW  (UIWindow *)[[[UIApplication sharedApplication] windows]lastObject]



/*-------------------文件路径--------------------*/
#define DocumentsPath [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/"]

CG_INLINE UIImage* scaleImageByFactor(UIImage* image, float scaleSize)
{
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

CG_INLINE UIImage* scaleImageBySize(UIImage* image, float width, float height)
{
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    [image drawInRect:CGRectMake(0, 0, width, height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

CG_INLINE int leftOfView(UIView* v)
{
    return v.frame.origin.x;
}
CG_INLINE int topOfView(UIView* v)
{
    return v.frame.origin.y;
}
CG_INLINE int rightOfView(UIView* v)
{
    return v.frame.origin.x + v.frame.size.width;
}
CG_INLINE int bottomOfView(UIView* v)
{
    return v.frame.origin.y + v.frame.size.height;
}

CG_INLINE int leftOfRect(CGRect rc)
{
    return rc.origin.x;
}
CG_INLINE int topOfRect(CGRect rc)
{
    return rc.origin.y;
}
CG_INLINE int rightOfRect(CGRect rc)
{
    return rc.origin.x + rc.size.width;
}
CG_INLINE int bottomOfRect(CGRect rc)
{
    return rc.origin.y + rc.size.height;
}


#define INVALID_SENSOR_VALUE (-9999.00f)

#endif /* _cn_joyway_lib_misc_CommonDefine_h_ */
