package tdh.utils
{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import flash.display.DisplayObjectContainer;
	
	public class BitmapInt extends Object
	{
		
		public function BitmapInt(bitmapData:BitmapData = null)
		{
			super();
			this.cache = new Vector.<BitmapData>();
			this.bitmapData = bitmapData;
		}
		
		private const sprite:Sprite = new Sprite();
		
		private const bitmap:Bitmap = new Bitmap();
		
		private var src:BitmapData;
		
		private var cache:Vector.<BitmapData>;
		
		private var _width:int;
		
		private var _height:int;
		
		public var reference:Boolean = true;
		
		public function clearSrc():void
		{
			this.src.dispose();
			var i:int = 0;
			while (i < 9)
			{
				this.cache[i].dispose();
				i++;
			}
		}
		
		public function set bitmapData(value:BitmapData):void
		{
			var i:* = 0;
			if (value != null)
			{
				this.src = this.reference ? value : value.clone();
				this._width = this.src.width / 10;
				this._height = this.src.height;
				i = 1;
				while (i < 10)
				{
					this.cache.push(new BitmapData(this._width * i, this._height));
					i++;
				}
			}
		}
		
		public var autoWH:Boolean;
		
		private var _value:int;
		
		public function set value(value:int):void
		{
			var rect:Rectangle = null;
			var dest:Point = null;
			var i:* = 0;
			var p:* = 0;
			var a:* = 0;
			var b:* = 0;
			var len:* = 0;
			if (this._value == value)
			{
				return;
			}
			if (this.src != null)
			{
				rect = new Rectangle(0, 0, this._width, this._height);
				dest = new Point();
				if (value == 0)
				{
					this.bitmap.bitmapData = this.cache[0];
					this.bitmap.bitmapData.copyPixels(this.src, rect, dest);
					this._value = value;
					return;
				}
				i = 10;
				while (i > 0)
				{
					p = Math.pow(10, i - 1);
					if (value >= p)
					{
						len = i;
						this.bitmap.bitmapData = this.cache[i - 1];
						break;
					}
					i--;
				}
				while (--i > -1)
				{
					p = Math.pow(10, i);
					a = int(value / p % 10);
					dest.x = (len - i - 1) * this._width;
					rect.x = a * this._width;
					this.bitmap.bitmapData.copyPixels(this.src, rect, dest);
				}
			}
			this._value = value;
		}
		
		public function get value():int
		{
			return this._value;
		}
		
		public function showAs(parent:DisplayObjectContainer, useSprite:Boolean = false):void
		{
			this._useSPrite = useSprite
			if (this._useSPrite)
			{
				this.sprite.addChild(this.bitmap);
				parent.addChild(this.sprite);
			}
			else
			{
				parent.addChild(this.bitmap);
			}
		}
		
		private var _useSPrite:Boolean;
		
		public function set x(value:Number):void
		{
			if (this._useSPrite)
			{
				this.sprite.x = value;
			}
			else
			{
				this.bitmap.x = value;
			}
		}
		
		public function set y(value:Number):void
		{
			if (this._useSPrite)
			{
				this.sprite.y = value;
			}
			else
			{
				this.bitmap.y = value;
			}
		}
		
		public function get x():Number
		{
			return this._useSPrite ? this.sprite.x : this.bitmap.x;
		}
		
		public function get y():Number
		{
			return this._useSPrite ? this.sprite.y : this.bitmap.y;
		}
		
		public function get width():Number
		{
			return this.bitmap.width;
		}
		
		public function set visible(value:Boolean):void
		{
			this.bitmap.visible = this.sprite.visible = value;
		}
		
		public function get visible():Boolean
		{
			return this._useSPrite ? this.sprite.visible : this.bitmap.visible;
		}
	}
}
