package com.bit101.web20
{
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.GradientType;
    import flash.geom.Matrix;
    import flash.filters.DropShadowFilter;

    public class GradientBar extends Sprite
    {
        private var _color:uint;
        private var _width:Number;
        private var _height:Number;
        private var _round:Number;
        private var _base:Shape;
        private var _gradient:Shape;
        private var _reflection:Shape;
        
        public function GradientBar(color:uint, w:Number, h:Number, round:Number=5)
        {
            _color = color;
            _width = w;
            _height = h;
            _round = round;
            init();
        }
        
        private function init():void
        {
            _base = new Shape();
            addChild(_base);
            _gradient = new Shape();
            addChild(_gradient);
            _reflection = new Shape();
            addChild(_reflection);
            
            shadowType = "outer";
            draw();
        }
        
        public function draw():void
        {
            drawBase();
            drawGradient();
            drawReflection();
        }
        
        private function drawBase():void
        {
            _base.graphics.clear();
            _base.graphics.beginFill(_color);
            _base.graphics.drawRoundRect(0, 0, _width, _height, _round, _round);
            _base.graphics.endFill();
        }
        
        private function drawGradient():void
        {
            _gradient.graphics.clear();
            var matrix:Matrix = new Matrix();
            matrix.createGradientBox(_width, _height, Math.PI / 2, 0, 0);
            _gradient.graphics.beginGradientFill(GradientType.LINEAR, [0xffffff, 0xffffff], [.5, 0], [0, 255], matrix);
            _gradient.graphics.drawRoundRect(0, 0, _width, _height, _round, _round);
            _gradient.graphics.endFill();
        }
        
        private function drawReflection():void
        {
            _reflection.graphics.clear();
            var matrix:Matrix = new Matrix();
            matrix.createGradientBox(_width, _height * .45, Math.PI / 2, 0, 0);
            _reflection.graphics.beginGradientFill(GradientType.LINEAR, [0xffffff, 0xffffff], [.2, .13], [0, 255], matrix);
            _reflection.graphics.drawRoundRectComplex(0, 0, _width, _height * .45, _round / 2, _round / 2, 0, 0);
            _reflection.graphics.endFill();
        }
        
        public function set shadowType(type:String):void
        {
            if(type == "outer")
            {
                filters = [new DropShadowFilter(2, 90, 0, 1, 3, 3, .3)];
            }
            else if(type == "inner")
            {
                filters = [new DropShadowFilter(2, 90, 0, 1, 3, 3, .3, 1, true)];
            }
            else
            {
                filters = [];
            }
        }
        
        public function set color(c:uint):void
        {
            _color = c;
            draw();
        }
        public function get color():uint
        {
            return _color;
        }
    }
}