using UnityEngine;
using FairyGUI.Utils;
namespace FairyGUI
{
    /// 
    /// GImage class.
    /// 
    public class GImage : GObject, IColorGear
    {
        Image _content;
        public GImage()
        {
        }
        override protected void CreateDisplayObject()
        {
            _content = new Image();
            _content.gOwner = this;
            displayObject = _content;
        }
        /// 
        /// Color of the image. 
        /// 
        public Color color
        {
            get { return _content.color; }
            set
            {
                _content.color = value;
                UpdateGear(4);
            }
        }
        /// 
        /// Flip type.
        /// 
        /// 
        public FlipType flip
        {
            get { return _content.graphics.flip; }
            set { _content.graphics.flip = value; }
        }
        /// 
        /// Fill method.
        /// 
        /// 
        public FillMethod fillMethod
        {
            get { return _content.fillMethod; }
            set { _content.fillMethod = value; }
        }
        /// 
        /// Fill origin.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public int fillOrigin
        {
            get { return _content.fillOrigin; }
            set { _content.fillOrigin = value; }
        }
        /// 
        /// Fill clockwise if true.
        /// 
        public bool fillClockwise
        {
            get { return _content.fillClockwise; }
            set { _content.fillClockwise = value; }
        }
        /// 
        /// Fill amount. (0~1)
        /// 
        public float fillAmount
        {
            get { return _content.fillAmount; }
            set { _content.fillAmount = value; }
        }
        /// 
        /// Set texture directly. The image wont own the texture.
        /// 
        public NTexture texture
        {
            get { return _content.texture; }
            set
            {
                if (value != null)
                {
                    sourceWidth = value.width;
                    sourceHeight = value.height;
                }
                else
                {
                    sourceWidth = 0;
                    sourceHeight = 0;
                }
                initWidth = sourceWidth;
                initHeight = sourceHeight;
                _content.texture = value;
            }
        }
        /// 
        /// Set material.
        /// 
        public Material material
        {
            get { return _content.material; }
            set { _content.material = value; }
        }
        /// 
        /// Set shader.
        /// 
        public string shader
        {
            get { return _content.shader; }
            set { _content.shader = value; }
        }
        override public void ConstructFromResource()
        {
            this.gameObjectName = packageItem.name;
            
            PackageItem contentItem = packageItem.getBranch();
            sourceWidth = contentItem.width;
            sourceHeight = contentItem.height;
            initWidth = sourceWidth;
            initHeight = sourceHeight;
            contentItem = contentItem.getHighResolution();
            contentItem.Load();
            _content.scale9Grid = contentItem.scale9Grid;
            _content.scaleByTile = contentItem.scaleByTile;
            _content.tileGridIndice = contentItem.tileGridIndice;
            _content.texture = contentItem.texture;
            _content.textureScale = new Vector2(contentItem.width / (float)sourceWidth, contentItem.height / (float)sourceHeight);
            SetSize(sourceWidth, sourceHeight);
        }
        override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
        {
            base.Setup_BeforeAdd(buffer, beginPos);
            buffer.Seek(beginPos, 5);
            if (buffer.ReadBool())
                _content.color = buffer.ReadColor();
            _content.graphics.flip = (FlipType)buffer.ReadByte();
            _content.fillMethod = (FillMethod)buffer.ReadByte();
            if (_content.fillMethod != FillMethod.None)
            {
                _content.fillOrigin = buffer.ReadByte();
                _content.fillClockwise = buffer.ReadBool();
                _content.fillAmount = buffer.ReadFloat();
            }
        }
    }
}