//
//  libs.h
//  汎用関数
//
//  Created by AIG-Soft on 10/12/21.
//  Copyright 2010 AIG-Soft. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>	// .layerを使うため
#include <stdlib.h>	// abs()

typedef enum { // デバイス種類
	DEVICE_UNKNOWN=0,
	DEVICE_IPAD,
	DEVICE_IPODTOUCH,
	DEVICE_IPHONE,
	// シミュレーター
	DEVICE_IPHONE_SIMURATOR,
	DEVICE_IPAD_SIMULATOR
} DEVICE_TYPE;
#define DEVICE_SIMULATOR	DEVICE_IPHONE_SIMURATOR

#ifdef UIORIENTATION
#define UIOrientationUnknown			(UIInterfaceOrientation)UIDeviceOrientationUnknown
#define UIOrientationPortrait			UIInterfaceOrientationPortrait
#define UIOrientationPortraitUpsideDown	UIInterfaceOrientationPortraitUpsideDown
#define UIOrientationLandscapeLeft		UIInterfaceOrientationLandscapeRight	// 逆のように見えるが、UIApplication.hにおける定義でこうなってる
#define UIOrientationLandscapeRight		UIInterfaceOrientationLandscapeLeft		// 逆のように見えるが、UIApplication.hにおける定義でこうなってる
#define
#else
#define UIOrientationUnknown			UIDeviceOrientationUnknown
#define UIOrientationPortrait			UIDeviceOrientationPortrait
#define UIOrientationPortraitUpsideDown	UIDeviceOrientationPortraitUpsideDown
#define UIOrientationLandscapeLeft		UIDeviceOrientationLandscapeLeft
#define UIOrientationLandscapeRight		UIDeviceOrientationLandscapeRight
#endif

// プロトタイプ宣言
void SetViewSize(UIView *view,CGFloat wx,CGFloat wy);
NSString *strInterfaceOrientation(UIInterfaceOrientation Orientation);
NSString *strDeviceOrientation(UIDeviceOrientation Orientation);
BOOL checkDeviceOrientation(UIDeviceOrientation *deviceOrientation);
BOOL checkDeviceOrientationOK(void);
NSUInteger getDeviceRotation(void);
void understandOrientation(UIInterfaceOrientation Orientation);
#ifdef UIORIENTATION
UIInterfaceOrientation getOrientation(void);
#else
UIDeviceOrientation getOrientation(void);
#endif
NSString *mesONOFF(BOOL onoff);
NSString *mesONOFF2(BOOL onoff,NSString *onmes,NSString *offmes);

NSString *getDevicePlatform(void);
NSString *getDeviceModelName(void);
DEVICE_TYPE getDeviceType(void);
DEVICE_TYPE getDeviceType2(void);
BOOL isDeviceIpad(DEVICE_TYPE devtype);
BOOL isDeviceIpadAndPortrait(DEVICE_TYPE devtype);

NSUInteger getHourMinSec(NSDate *date,NSUInteger *hour,NSUInteger *sec);
NSString *dateStringWithoutGMT(NSDate *date);
void leftString(NSMutableString *spc,NSString *title,BOOL flocalize);
void leftString2(NSMutableString *spc,NSString *title,BOOL flocalize,UITextView *textView);
CGSize sizeWithFont(NSString *str,UIFont *font,int fdescender);
void setActionSheetResize(UIActionSheet *asheet);
void setAlertViewResize(UIAlertView *aview);
void Alert1(NSString *title,NSString *message,id delegate);
NSString *dateFormatJtoE(NSString *date);

BOOL cmpRegularExpression(NSString *str,NSString *expression);
BOOL isMailAddress(NSString *adrs);
BOOL isServerAddress(NSString *adrs);
NSString *localKeyName(NSString *defaultName);
UIColor *colorByNo(NSInteger no);

double getSystemVersion(void);
CGFloat distance(CGPoint sp,CGPoint ep);
//
BOOL isPointInRect(CGPoint point,CGRect rect);
NSInteger strArray2No(NSString *str,NSArray *array);
NSInteger pathArray2No(NSString *fname,NSArray *pathes);
NSString *makeNowDateStr(void);
CGSize getFontSize(UIFont *font);
NSInteger countCharInString(NSString *str,ULong chr);

#if 0
NSString *CstringToNSString(char *mes);
#define	uCstringToNSString(mes) CstringToNSString((char *)mes)
#else
//#define	 CstringToNSString(mes)	[NSString stringWithCString:mes         encoding:NSUTF8StringEncoding]
//#define	uCstringToNSString(mes)	[NSString stringWithCString:(char *)mes encoding:NSUTF8StringEncoding]
#define	 CstringToNSString(mes)	@(mes)				/* これだけでOK */
#define	uCstringToNSString(mes)	@((char *)(mes))	/* これだけでOK */
#endif
#define nsStringTocString(str)	( char *)[str UTF8String]
#define unsStringTocString(str)	(UByte *)[str UTF8String]
size_t setLineStyleWith16bit(NSUInteger style,CGFloat lengths[]);
UIImage *rotateImage(UIImage *img,int degree);
UIImage *resizedImage(UIImage *img,CGSize size);
UIImage *imageFromView(UIView *view);
NSString *getLanguage(void);
BOOL isLocalizeJapan(void);
void setColor(CGContextRef context,int r,int g,int b,int alpha);
void setLineStyle(CGContextRef context,NSUInteger style);
void strokeOrFill(CGContextRef context,BOOL ffill);
void EnterRunLoop(NSTimeInterval tm);
#define	EnterRunLoopWithoutPool(tm)	[[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow:(tm)]]
#define DoAnimation()	EnterRunLoop(0.0)

// 実行ループにタイマーを追加する
#define AddTimerToRunLoop(timer)	[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]

CGContextRef MyCreateBitmapContext(int width,int height,CGBitmapInfo bitmapInfo);
void UIGraphicsPushContext2(CGContextRef context);
void UIGraphicsPopContext2(void);
void UIGraphicsRotate180(CGContextRef context,UIView *view);
CGRect getRetinaSize(CGRect rect);
void UIGraphicsBeginImageContextRetina(CGSize size);
UByte *memclr(UByte *bp,size_t size);
UWord rd68w(UByte *b);
ULong rd68l(UByte *b);

void utf16ToSurrogate(ULong utf16,UWord *hi,UWord *lo);
ULong surrogateToUtf16(UWord hi,UWord lo);
BOOL isSurrogate(ULong utf16);

//----------------------------

// 1ピクセルのバイト数
#define PIXELBYTE	(4)	// R/G/B/alphaの8bit*4 = ULongであること

typedef struct {
	int	r;
	int	g;
	int	b;
} RGB_TABLE;

typedef struct {
	int	r;
	int	g;
	int	b;
	int	alpha;
} rgba_color;

#define rgba_color2conpositeRGBA(rgba)	((rgba.r<<24)|(rgba.g<<16)|(rgba.b<<8)|(rgba.alpha))

#define printRGB(rgb)	nslog(@"r=%d/g=%d/b=%d/a=%d",rgb.r,rgb.g,rgb.b,rgb.alpha)

rgba_color getWithStandardColor(int r,int g,int b,int alpha_);

#define _cf(c) ((CGFloat)(c)/255.0f)

// 0-255で指定する関数
#ifndef RGB
#define RGB(R,G,B) 		RGBA(R,G,B,255)
#endif
#ifndef RGBA
#define RGBA(R,G,B,A)	[UIColor colorWithRed:_cf(R) green:_cf(G) blue:_cf(B) alpha:_cf(A)]
#define RGBA2(RGBA)		[UIColor colorWithRed:_cf(RGBA.r) green:_cf(RGBA.g) blue:_cf(RGBA.b) alpha:_cf(RGBA.alpha)]
#endif

enum {
	PENCIL_COLOR_BLACK=0,
	PENCIL_COLOR_BLUE,
	PENCIL_COLOR_RED,
	PENCIL_COLOR_MAGENTA,
	PENCIL_COLOR_GREEN,
	PENCIL_COLOR_CYAN,
	PENCIL_COLOR_YELLOW,
	PENCIL_COLOR_WHITE
};

enum { // 標準色指定
	TBLACK=0,
	TBLUE,
 	TRED,
 	TMAGENTA,
 	TGREEN,
 	TCYAN,
 	TYELLOW,
 	TWHITE,
	// 以下highlight色
	THBLACK,
 	THBLUE,
 	THRED,
 	THMAGENTA,
 	THGREEN,
 	THCYAN,
 	THYELLOW,
 	THWHITE,
};

UIColor *standardColors(NSInteger col);
rgba_color getStandardColorTable(NSInteger col);


// #define関数
// 実行ループにタイマーを追加する
#define AddTimerToRunLoop(timer)	[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]

// 浮動小数点(<1.0,>=0.1)比較:浮動小数点では==は使えないので
#define	EquFloat(f1,f2)	(abs(f1-f2)<0.1)

// 分→秒変換
#define	tMin(m)	((NSTimeInterval)(m)*60.0)

// カメラが有効かどうか
#define	isCameraEnable()	([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
// アルバムが有効かどうか
#define	isAlbumEnable()		([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])

// センタリング
#define CenteringX(lbl,view)	{CGPoint center=lbl.center; center.x=view.center.x; lbl.center=center;}


// デバッグ用関数
void _printFrame(NSString *mes,CGRect frame);
void _printInset(NSString *mes,UIEdgeInsets inset);
void _printSize(NSString *mes,CGSize size);
void _printOffset(NSString *mes,CGPoint point);
#ifdef DEBUG
	#define printFrame(mes,frame)	_printFrame(mes,frame)
	#define	printInset(mes,inset)	_printInset(mes,inset)
	#define	printSize(mes,size)		_printSize(mes,size)
	#define	printOffset(mes,point)	_printOffset(mes,point)
	#define printRect(mes,rect)		_printFrame(mes,rect)
	#define printPoint(mes,point)	_printOffset(mes,point)
	#define	isJIKKI()				(getDeviceType()<DEVICE_SIMULATOR)
	#define	isSIMULATOR()			(getDeviceType()>=DEVICE_SIMULATOR)
	#if TARGET_IPHONE_SIMULATOR
	#import <objc/objc-runtime.h>
	#else
	#import <objc/runtime.h>
	#endif
	void dumpSubView(UIView *view);
#else // 本番
	#define	printFrame(mes,frame)
	#define	printInset(mes,inset)
	#define	printSize(mes,size)
	#define	printOffset(mes,point)
	#define printRect(mes,rect)
	#define printPoint(mes,point)
	#define	isJIKKI()				(1)	/* 本番は常に実機とみなす */
	#define	isSIMULATOR()			(0)	/* 同上 */
	#define	dumpSubView(vw)
#endif
#if COMPILE_MODE==2 // Distributionの時は外す
	#define printFrame2(mes,frame)
	#undef printInset
	#define	printInset(mes,inset)
	#define	printOffset2(mes,point)
#else
	// 常に実体を呼び出す
	#define printFrame2(mes,frame)	_printFrame(mes,frame)
	#define	printInset2(mes,inset)	_printInset(mes,inset)
	#define	printOffset2(mes,point)	_printOffset(mes,point)
#endif
