import frameData from './frameData';
import maskCreater from './maskCreater';		//mask creater

/**
 * 
 * @method 
 * @desc   -  a creater for game view
 * @param argument{
 *		@param {Object}  game         - find game object 				
 *		@param {Object}  loader       - find game loader		
 *		@param {Object}  layout       - find layout object		
 *		@param {Boolean} sprite       - is sprite ? 
 *		@param {Object}  circle       - draw circle param 
 *		@param {String}  layId        - layout id 
 *		@param {String}  imgId        - img id
 *		@param {Number}  x            - obj x-pos
 *		@param {Number}  y            - obj y-pos
 *		@param {Number}  width        - createjs rect width
 *	    @param {Number}  height       - createjs rect height
 *	    @param {String}  bg           - createjs rect background
 *		@param {Number}  regX         - animation orgin x
 *		@param {Number}  regY         - animation orgin y
 *		@param {Number}  scaleX       - obj scale x
 *		@param {Number}  scaleY       - obj scale y
 *		@param {Number}  moved        - obj can move or no
 *		@param {Number}  vx           - obj x move speed
 *		@param {Number}  alpha        - obj alpha
 *		@param {Number}  type         - obj type --- 1 normal / 2 ding / 3 break / 4 word-1 / 5 word -2
 *		@param {Boolean} visible      - obj visible
 *		@param {Number}  framesName   - sprite frames name
 *		@param {String}  aniName      - sprite name
 *		@param {String}  ani_1        - sprite aniamtions name - 1
 *		@param {String}  container    - addchild container
 *		@param {String}  textBaseline - text base line
 *	    @param {String}  textAlign    - text align
 *	    @param {String}  textColor    - text color
 *	    @param {String}  textSize     - text size
 *	    @param {Boolean} shadow       - show text shadow true or false
 *	    @param {Boolean} shadowColor  - text shadow color
 *	    @param {Boolean} shadowOffsetX- text shadow offsetX
 *	    @param {Boolean} shadowOffsetY- text shadow offsetY
 *	    @param {Boolean} shadowBlur   - text shadow blur
 *	    @param {Number}  cacheX       - cache Text X
 *		@param {Number}  cacheY       - cache Text Y
 *		@param {Number}  cacheW       - cache Text width
 *		@param {Number}  cacheH       - cache Text height
 *	}
 *	
 */


const creater = function (argument) {
	let _default = {
		game: null,
		loader: null,
		layout: null,
		sprite: false,
		single: false,
		empty: false,
		layId: '',
		imgId: '',
		name: '',
		ox: 0,
		oy: 0,
		x: 0,
		y: 0,
		width: 750,
		height: 1334,
		rotation: 0,
		bg: 'rgba(255,255,255,1)',
		regX: 0,
		regY: 0,
		visible: true,
		hide: false,
		moved: 0,
		vx: 1,
		circle: {
			visible: false,
			sector: false,
			startAngle: 0,
			endAngle: Math.PI * 2,
		},
		circleLine: {
			visible: false,
			borderWidth: 2,
			color: '#ffffff',
		},
		mask: {
			visible: false,
			x: 0,
			y: 0,
			r: 0,
			p: null,
			debug: false
		},
		scaleX: 1,
		scaleY: 1,
		alpha: 1,
		type: 0,
		framesName: '',
		defaultAni: 'default',
		container: '',
		rect: {
			x: 0,
			y: 0,
			borderWidth: 1,
			p: null,
			blurFilter: false
		},
		text: {
			visible: false,
			word: "",
			baseline: "alphabetic",
			align: "center",
			color: "#ffffff",
			size: "26px fzzt",
			lineHeight: "30"
		},
		singleSprite: {
			visible: false,
			parent: {},
			name: ''
		},
		shadow: false,
		shadowColor: '#dddddd',
		roCenter: false,
		shadowOffsetX: 0,
		shadowOffsetY: 0,
		shadowBlur: 2,
		cacheX: 0,
		cacheY: 0,
		cacheW: 750,
		cacheH: 1334,
		return: false,
		mouseEnabled: true,
		compositeOperation: ''
	}

	let maskObj = Object.assign({}, _default.mask, argument.mask);
	let textObj = Object.assign({}, _default.text, argument.text);
	let rectObj = Object.assign({}, _default.rect, argument.rect);
	let circleObj = Object.assign({}, _default.circle, argument.circle);
	let singleSpriteObj = Object.assign({}, _default.singleSprite, argument.singleSprite);
	let circleLineObj = Object.assign({}, _default.circleLine, argument.circleLine);


	let opts = Object.assign({}, _default, argument);
	opts.mask = maskObj;
	opts.text = textObj;
	opts.rect = rectObj;
	opts.circle = circleObj;
	opts.circleLine = circleLineObj;
	opts.singleSprit = singleSpriteObj;

	// console.log(opts.layId,opts.imgId)
	//雪碧图单帧
	if (opts.singleSprite.visible) {
		// console.log(opts.singleSprite.parent,opts.singleSprite.name)
		opts.layout[opts.layId] = new createjs.Sprite(opts.singleSprite.parent.spriteSheet, opts.singleSprite.name).set({
			x: opts.x + (opts.roCenter ? opts.width / 2 : 0),
			y: opts.y + (opts.roCenter ? opts.height / 2 : 0),
			ox: opts.ox ? opts.ox : opts.x + (opts.roCenter ? opts.width / 2 : 0),
			oy: opts.oy ? opts.oy : opts.y + (opts.roCenter ? opts.height / 2 : 0),
			regX: opts.regX + (opts.roCenter ? opts.width / 2 : 0),
			regY: opts.regY + (opts.roCenter ? opts.height / 2 : 0),
			type: opts.type,
			tindex: opts.tindex,
			hide: opts.hide,
			state: opts.state,
			scaleX: opts.scaleX,
			scaleY: opts.scaleY,
			rotation: opts.rotation,
			width: opts.width,
			height: opts.height,
			name: opts.name || opts.layId,
			moved: opts.moved,
			vx: opts.vx,
			sp: opts.sp,
			hs: opts.hs,
			alpha: opts.alpha,
			visible: opts.visible,
			mask: opts.mask.visible ? shape : null,
			mouseEnabled: opts.mouseEnabled
		});
		if (opts.maskAnimation) {
			opts.maskContainer = new createjs.Container();

			if (opts.transform)
				opts.layout[opts.layId].setTransform(...opts.transform);


			opts.maskContainer.mask = new maskCreater(opts.mask.name, opts.mask.type, opts.mask.transform);
			opts.maskContainer.compositeOperation = opts.compositeOperation;
			opts.maskContainer.addChild(opts.layout[opts.layId]);
		}
	}
	//帧动画
	else if (opts.sprite) {
		let img = opts.loader.getResult(opts.imgId);
		var spriteSheet = new createjs.SpriteSheet({
			images: [img],
			frames: frameData[opts.framesName].frames,
			animations: opts.animations
		});

		opts.layout[opts.layId] = new createjs.Sprite(spriteSheet, opts.defaultAni).set({
			x: opts.x + (opts.roCenter ? opts.width / 2 : 0),
			y: opts.y + (opts.roCenter ? opts.height / 2 : 0),
			// width:  frameData[opts.framesName].sourceW,
			// height: frameData[opts.framesName].sourceH,
			ox: opts.ox ? opts.ox : opts.x + (opts.roCenter ? opts.width / 2 : 0),
			oy: opts.oy ? opts.oy : opts.y + (opts.roCenter ? opts.height / 2 : 0),
			regX: opts.regX + (opts.roCenter ? opts.width / 2 : 0),
			regY: opts.regY + (opts.roCenter ? opts.height / 2 : 0),
			type: opts.type,
			state: opts.state,
			scaleX: opts.scaleX,
			scaleY: opts.scaleY,
			name: opts.name || opts.layId,
			compositeOperation: opts.compositeOperation,
			alpha: opts.alpha
		});
	} else if (opts.maskAnimation) {
		//遮罩动画
		opts.maskContainer = new createjs.Container();
		let img = opts.loader.getResult(opts.imgId);

		opts.layout[opts.layId] = new createjs.Bitmap(img).setTransform(...opts.transform).set({
			alpha: opts.alpha,
			ox: opts.ox,
			oy: opts.oy
		});

		opts.maskContainer.mask = new maskCreater(opts.mask.name, opts.mask.type, opts.mask.transform);
		opts.maskContainer.compositeOperation = opts.compositeOperation;
		opts.maskContainer.addChild(opts.layout[opts.layId]);

	} else if (opts.text.visible) {
		//文字
		let shape;
		if (opts.mask.visible) {
			let g = new createjs.Graphics();
			shape = new createjs.Shape().set({
				graphics: g.drawRect(opts.mask.x, opts.mask.y, opts.mask.width, opts.mask.height),
				x: opts.mask.x,
				y: opts.mask.y,
			});
		}
		let shadow = opts.shadow ? new createjs.Shadow(opts.shadowColor, opts.shadowOffsetX, opts.shadowOffsetY, opts.shadowBlur) : null;

		opts.layout[opts.layId] = new createjs.Text(opts.text.word, opts.text.size, opts.text.color).set({
			x: opts.x,
			y: opts.y,
			ox: opts.ox ? opts.ox : opts.x,
			oy: opts.oy ? opts.oy : opts.y,
			regX: opts.regX,
			regY: opts.regY,
			scaleX: opts.scaleX,
			scaleY: opts.scaleY,
			alpha: opts.alpha,
			shadow: shadow,
			name: opts.name || opts.layId,
			mask: opts.mask.visible ? shape : null,
			lineHeight: opts.text.lineHeight,
			textBaseline: opts.text.baseline,
			textAlign: opts.text.align,
			mouseEnabled: opts.mouseEnabled
		})
		// console.log(opts.layout[opts.layId])
		opts.game.webGL && opts.cacheContainer.addChild(opts.layout[opts.layId])
		opts.game.webGL && opts.cacheContainer.cache(opts.cacheX, opts.cacheY, opts.cacheW, opts.cacheH);
	} else if (opts.rect.visible) {
		//Graphics rect
		let g = new createjs.Graphics();

		// if (opts.mask.visible) {
		// 	let g2 = new createjs.Graphics();
		// 	shape = new createjs.Shape().set({
		// 		graphics: opts.mask.p ? g2.p(opts.mask.p) : g2.drawRect(opts.mask.x, opts.mask.y, opts.mask.width, opts.mask.height),
		// 		x: opts.x,
		// 		y: opts.y,
		// 	});
		// }

		let graphics;
		if (opts.rect.blurFilter) {
			graphics = g.beginBitmapFill(opts.loader.getResult(opts.imgId), 'no-repeat')
				.drawRect(opts.rect.x, opts.rect.y, opts.rect.width, opts.rect.height)

		} else {
			// console.log(opts.rect)
			if (opts.rect.round && opts.rect.roundLine)
				graphics = g.setStrokeStyle(opts.rect.borderWidth).f(opts.bg).s(opts.rect.bg).drawRoundRect(opts.rect.x, opts.rect.y, opts.rect.width, opts.rect.height, opts.rect.r).es();
			else if (opts.rect.round)
				graphics = g.f(opts.bg).drawRoundRect(opts.rect.x, opts.rect.y, opts.rect.width, opts.rect.height, opts.rect.r)
			else if (opts.rect.roundLine)
				graphics = g.setStrokeStyle(opts.rect.borderWidth).f(opts.bg).s(opts.rect.bg).drawRect(opts.rect.x, opts.rect.y, opts.rect.width, opts.rect.height).es();
			else if (opts.rect.p)
				graphics = g.p(opts.rect.p);
			else
				graphics = g.f(opts.bg).drawRect(opts.rect.x, opts.rect.y, opts.rect.width, opts.rect.height);
		}

		//create shape
		opts.layout[opts.layId] = new createjs.Shape().set({
			graphics: graphics,
			x: opts.x,
			y: opts.y,
			regX: opts.regX,
			regY: opts.regY,
			alpha: opts.alpha,
			name: opts.name || opts.layId,
			mouseEnabled: opts.mouseEnabled
		});

		opts.layout[opts.layId].mask = new maskCreater(opts.mask.name, opts.mask.type, opts.mask.transform);

		if (opts.rect.blurFilter) {
			let blurFilter = new createjs.BlurFilter(10, 10, 10);
			opts.layout[opts.layId].filters = [blurFilter];
			opts.layout[opts.layId].cache(opts.rect.x, opts.rect.y, opts.rect.width, opts.rect.height);
		}

		opts.game.webGL && opts.cacheContainer.addChild(opts.layout[opts.layId])
		opts.game.webGL && opts.cacheContainer.cache(opts.cacheX, opts.cacheY, opts.cacheW, opts.cacheH);
	}
	else if (opts.circle.visible) {
		//Graphics circle
		let g = new createjs.Graphics();
		let angle, startAngle, endAngle, x1 = 0, y1 = 0;
		if (opts.circle.sector) {	//画扇形
			angle = (Math.abs(opts.circle.angle) > 360) ? 360 : opts.circle.angle;
			startAngle = opts.circle.startAngle * Math.PI / 180;
			endAngle = opts.circle.startAngle + angle * Math.PI / 180;
			x1 = opts.circle.x + opts.circle.r * Math.cos(startAngle);
			y1 = opts.circle.x + opts.circle.r * Math.sin(startAngle);
		}

		opts.layout[opts.layId] = new createjs.Shape().set({
			graphics: g.f(opts.bg).moveTo(opts.circle.x, opts.circle.y).arc(opts.circle.x, opts.circle.y, opts.circle.r, startAngle ? startAngle : 0, endAngle ? endAngle : Math.PI * 2).endFill(),
			x: opts.x + (opts.roCenter ? opts.circle.r : 0),
			y: opts.y + (opts.roCenter ? opts.circle.r : 0),
			regX: opts.regX + (opts.roCenter ? opts.circle.r : 0),
			regY: opts.regY + (opts.roCenter ? opts.circle.r : 0),
			rotation: opts.rotation,
			alpha: opts.alpha,
			name: opts.name || opts.layId,
			mouseEnabled: opts.mouseEnabled
		});
		// opts.game.webGL && opts.cacheContainer.addChild(opts.layout[opts.layId])
		// opts.game.webGL && opts.cacheContainer.cache(opts.cacheX,opts.cacheY,opts.cacheW,opts.cacheH);
	}
	else if (opts.circleLine.visible) {
		//circle line
		let g = new createjs.Graphics();

		opts.layout[opts.layId] = new createjs.Shape().set({
			graphics: g.setStrokeStyle(opts.circleLine.borderWidth).s(opts.circleLine.color).arc(opts.circleLine.x, opts.circleLine.y, opts.circleLine.r, opts.circleLine.startAngle, opts.circleLine.endAngle).endFill(),
			x: opts.x + (opts.roCenter ? opts.circleLine.r : 0),
			y: opts.y + (opts.roCenter ? opts.circleLine.r : 0),
			regX: opts.regX + (opts.roCenter ? opts.circleLine.r : 0),
			regY: opts.regY + (opts.roCenter ? opts.circleLine.r : 0),
			rotation: opts.rotation,
			alpha: opts.alpha,
			name: opts.name || opts.layId,
			mouseEnabled: opts.mouseEnabled
		});

		// opts.game.webGL && opts.cacheContainer.addChild(opts.layout[opts.layId])
		// opts.game.webGL && opts.cacheContainer.cache(opts.cacheX,opts.cacheY,opts.cacheW,opts.cacheH);
	}
	else if (opts.bitmapShap) {
		let g = new createjs.Graphics();
		let img = opts.loader.getResult(opts.imgId);
		opts.layout[opts.layId] = new createjs.Shape().set({
			graphics: g.beginBitmapFill(img, "no-repeat").drawRect(0, 0, opts.width, opts.height),
			x: opts.x,
			y: opts.y,
			scaleX: opts.scaleX,
			scaleY: opts.scaleY,
			alpha: opts.alpha,
			name: opts.name || opts.layId,
			mouseEnabled: false
		});
		// (opts.game.webGL || opts.game.needCache) && opts.cacheContainer.cache(opts.cacheX,opts.cacheY,opts.cacheW,opts.cacheH);
	}
	else if (opts.bitmapCircle) {
		let g = new createjs.Graphics();
		let img;
		if (typeof (opts.imgId) == 'object') {
			if (opts.imgId.other) {
				img = opts.game.friend.loadedPhoto;
			}
			else if (opts.imgId.fighter) {
				img = opts.imgId.fighter
			}
			else {
				img = opts.game.userinfo.loadedPhoto
			}
		} else {
			img = opts.loader.getResult(opts.imgId)
		}
		opts.layout[opts.layId] = new createjs.Shape().set({
			graphics: g.beginBitmapFill(img, "no-repeat").drawCircle(opts.circle.x, opts.circle.y, opts.circle.r),
			x: opts.x,
			y: opts.y,
			scaleX: opts.scaleX,
			scaleY: opts.scaleY,
			alpha: opts.alpha,
			name: opts.name || opts.layId,
			mouseEnabled: false
		});
		// (opts.game.webGL || opts.game.needCache) && opts.cacheContainer.cache(opts.cacheX,opts.cacheY,opts.cacheW,opts.cacheH);
	}
	else {
		//图
		let img = opts.loader.getResult(opts.imgId);
		//taikohui userphoto
		let shape;
		if (opts.mask.visible) {
			let g = new createjs.Graphics();
			shape = new createjs.Shape().set({
				graphics: g.drawRect(opts.mask.x, opts.mask.y, opts.mask.width, opts.mask.height),
				x: opts.x,
				y: opts.y,
			});
		}
		opts.layout[opts.layId] = new createjs.Bitmap(img).set({
			x: opts.x + (opts.roCenter ? opts.width / 2 : 0),
			y: opts.y + (opts.roCenter ? opts.height / 2 : 0),
			ox: opts.ox ? opts.ox : opts.x,
			oy: opts.oy ? opts.oy : opts.y,
			width: opts.width ? opts.width : img.width,
			height: opts.height ? opts.height : img.height,
			regX: opts.regX + (opts.roCenter ? opts.width / 2 : 0),
			regY: opts.regY + (opts.roCenter ? opts.height / 2 : 0),
			rotation: opts.rotation,
			visible: opts.visible,
			scaleX: opts.scaleX,
			scaleY: opts.scaleY,
			type: opts.type,
			moved: opts.moved,
			name: opts.name || opts.layId,
			vx: opts.vx,
			hide: opts.hide,
			state: opts.state,
			mask: opts.mask.visible ? shape : null,
			alpha: opts.alpha,
			mouseEnabled: opts.mouseEnabled
		});
	}

	if (opts.mask.visible) {
		let shape;
		let g = new createjs.Graphics();
		if (opts.mask.graph == 'rect') {
			shape = new createjs.Shape().set({
				graphics: g.drawRect(opts.mask.x, opts.mask.y, opts.mask.width, opts.mask.height),
				// x : opts.mask.x,
				// y : opts.mask.y,
			});
		}
		else if (opts.mask.graph == 'rectRound') {
			shape = new createjs.Shape().set({
				graphics: g.drawRoundRect(opts.mask.x, opts.mask.y, opts.mask.width, opts.mask.height, opts.mask.r)
			});
		}
		else {
			shape = new createjs.Shape().set({
				graphics: g.drawCircle(0, 0, opts.mask.r),
				x: opts.mask.x,
				y: opts.mask.y,
			});
		}

		opts.layout[opts.layId].mask = shape;
	}

	if (!opts.return) {
		let target = opts.layout[opts.layId];

		if ((opts.text.visible || opts.rect.visible) && opts.game.webGL) {
			target = opts.cacheContainer
		} else {
			opts.maskContainer
		}

		opts.container.addChild(target)
		// opts.container.addChild(opts.text.visible || opts.rect.visible ? opts.cacheContainer : opts.maskAnimation ? opts.maskContainer : opts.layout[opts.layId])
	}



	return opts.layout[opts.layId]
}
module.exports = creater;