using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIKit;
using Foundation;
using Shared.IO;
using CoreGraphics;

namespace Shared
{
	/// <summary>
	/// 文本输入框
	/// </summary>
	public class TextView : View
	{
		/// <summary>
		/// 文字大小
		/// </summary>
		/// <value>The size of the text.</value>
		public float TextSize
		{
			get
			{
				return (float)uILable.TextSize.PointSize;
			}
			set
			{
				uILable.TextSize = UIFont.SystemFontOfSize(value);
			}
		}

		int textID;
		/// <summary>
		/// 根据ID获取对应的备注
		/// </summary>
		public int TextID
		{
			get
			{
				return textID;
			}
			set
			{
				textID = value;
				Text = Language.StringByID(TextID);
			}
		}

		MyUILable uILable;
		public TextView()
		{
			uiView = uILable = new MyUILable(this) { };
           
		}

        string text = "";
		/// <summary>
		/// 文本
		/// </summary>
		/// <value>文本</value>
		public string Text
		{
			get
			{
                return text;
			}
			set
			{
                text = value;
                if(!IsCanRefresh){
                    return;
                }
				uILable.Text = value;
			}
		}

		/// <summary>
		/// 文本颜色
		/// </summary>
		public uint TextColor
		{
			get
			{
				nfloat r, g, b, a;
				uILable.TextColor.GetRGBA(out r, out g, out b, out a);
				r *= 255;
				g *= 255;
				b *= 255;
				a *= 255;
				return (uint)(r * 256 * 256 * 256 + g * 256 * 256 + b * 256 + a);
			}
			set
			{
				byte r, g, b, a;
				r = (byte)(value / 256 / 256 % 256);
				g = (byte)(value / 256 % 256);
				b = (byte)(value % 256);
				a = (byte)(value / 256 / 256 / 256 % 256);
				uILable.TextColor = UIKit.UIColor.FromRGBA(r, g, b, a);
			}
		}

        /// <summary>
        /// 是否支持换行
        /// </summary>
        /// <value><c>true</c> if is more lines; otherwise, <c>false</c>.</value>
        public bool IsMoreLines
        {
            get
            {
                return uILable.IsMoreLines;
            }
            set
            {
                uILable.IsMoreLines = value;
            }
        }

	
        public override void Refresh()
        {
            base.Refresh();
            Text = Text;
        }

	}

    class MyUILable : UILabel
    {
        [Weak] TextView textView;
        public MyUILable(TextView textView)
        {
            this.textView = textView;
            LineBreakMode = UILineBreakMode.MiddleTruncation;
            TextAlignment = UITextAlignment.Center;
        }

        bool isMoreLines;
        public bool IsMoreLines
        {
            get
            {
                return isMoreLines;
            }
            set
            {
                isMoreLines = value;
                if (isMoreLines)
                {
                    Lines = 0;
                }
                else
                {
                    Lines = 1;
                }
            }
        }

        /// <summary>
        /// 点击开始
        /// </summary>
        /// <param name="touches">Touches.</param>
        /// <param name="evt">Evt.</param>
        public override void TouchesBegan(NSSet touches, UIEvent evt)
        {
            textView?.TouchEvent(EventActions.Down, (touches.AnyObject as UITouch).LocationInView(this));
        }
        /// <summary>
        ///  移动
        /// </summary>
        /// <param name="touches">Touches.</param>
        /// <param name="evt">Evt.</param>
        public override void TouchesMoved(NSSet touches, UIEvent evt)
        {
            textView?.TouchEvent(EventActions.Move, (touches.AnyObject as UITouch).LocationInView(this));
        }

        /// <summary>
        /// 点击弹起
        /// </summary>
        /// <param name="touches">Touches.</param>
        /// <param name="evt">Evt.</param>
        public override void TouchesEnded(NSSet touches, UIEvent evt)
        {
            textView?.TouchEvent(EventActions.Up, (touches.AnyObject as UITouch).LocationInView(this));
        }

        public override void TouchesCancelled(NSSet touches, UIEvent evt)
        {
            textView?.TouchEvent(EventActions.Cancel, (touches.AnyObject as UITouch).LocationInView(this));
        }


        /// <summary>
        /// 因为这个视图很奇怪,会自动加了两个UIImageView,所以这个特殊处理一下
        /// </summary>
        /// <param name="view">View.</param>
        public override void AddSubview(UIView view)
        {
            if (view.GetType() == typeof(UIImageView) && view.Tag != int.MinValue)
            {
                return;
            }
            base.AddSubview(view);
        }


        /// <summary>
        /// 文本
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }

        /// <summary>
        /// 文本的颜色
        /// </summary>
        /// <value>文本的颜色</value>
        public UIColor TextColor
        {
            get
            {
                return base.TextColor;
            }
            set
            {
                base.TextColor = value;
            }
        }

        /// <summary>
        /// 文本的大小
        /// </summary>
        /// <value>文本的大小</value>
        public UIFont TextSize
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
            }
        }
    }

	//class MyUILable : UIScrollView
	//{
 //       View view; 
	//	public MyUILable(View view)
	//	{
	//		this.view = view;
	//		commonInit();
	//	}

	//	/// <summary>
	//	/// 点击开始
	//	/// </summary>
	//	/// <param name="touches">Touches.</param>
	//	/// <param name="evt">Evt.</param>
	//	public override void TouchesBegan(NSSet touches, UIEvent evt)
	//	{
	//		//base.TouchesBegan(touches, evt);
	//		view.TouchEvent(EventActions.Down, (touches.AnyObject as UITouch).LocationInView(this));
	//	}
	//	/// <summary>
	//	///  移动
	//	/// </summary>
	//	/// <param name="touches">Touches.</param>
	//	/// <param name="evt">Evt.</param>
	//	public override void TouchesMoved(NSSet touches, UIEvent evt)
	//	{
	//		//base.TouchesMoved(touches, evt);
	//		view.TouchEvent(EventActions.Move, (touches.AnyObject as UITouch).LocationInView(this));
	//	}



	//	/// <summary>
	//	/// 点击弹起
	//	/// </summary>
	//	/// <param name="touches">Touches.</param>
	//	/// <param name="evt">Evt.</param>
	//	public override void TouchesEnded(NSSet touches, UIEvent evt)
	//	{
	//		//base.TouchesEnded(touches, evt);
	//		view.TouchEvent(EventActions.Up, (touches.AnyObject as UITouch).LocationInView(this));
	//	}
		

	//	/// <summary>
	//	/// 因为这个视图很奇怪,会自动加了两个UIImageView,所以这个特殊处理一下
	//	/// </summary>
	//	/// <param name="view">View.</param>
	//	public override void AddSubview(UIView view)
	//	{
	//		if (view.GetType() == typeof(UIImageView) && view.Tag != int.MinValue)
	//		{
	//			return;
	//		}
	//		base.AddSubview(view);
	//	}

	//	UILabel[] labels = new UILabel[2];
	//	float scrollSpeed = 30;
	//	float bufferSpaceBetweenLabels = 20;
	//	void commonInit()
	//	{
	//		for (int i = 0; i < labels.Length; ++i)
	//		{
 //               labels[i] = new UILabel();
	//			labels[i].TextColor = UIColor.White;
	//			labels[i].BackgroundColor = UIColor.Clear;
 //               labels[i].Text = " ";
	//			AddSubview(labels[i]);
	//		}
	//		ShowsVerticalScrollIndicator = false;
	//		ShowsHorizontalScrollIndicator = false;
	//		UserInteractionEnabled = false;
	//	}


	//	void scroll()
	//	{
	//		ContentOffset = new CGPoint(0, 0);
	//		AnimateNotify(
	//			labels[0].Frame.Size.Width / scrollSpeed,
	//			0f,
	//			UIViewAnimationOptions.CurveLinear, () =>
	//			{
	//				ContentOffset = new CGPoint(labels[0].Frame.Width + bufferSpaceBetweenLabels, 0);
	//			},
	//			finished =>
	//			{
	//				if (finished)
	//					scroll();
	//			}
	//		);
	//	}

	//	void readjustLabels()
	//	{
	//		Layer.RemoveAllAnimations();
	//		float offset = 0.0f;
	//		for (int i = 0; i < labels.Length; ++i)
	//		{
	//			labels[i].SizeToFit();
	//			CGPoint center;
	//			center = labels[i].Center;
	//			center.Y = Center.Y - Frame.Y;
	//			labels[i].Center = center;

	//			CGRect frame;
	//			frame = labels[i].Frame;
	//			frame.X = offset;
	//			labels[i].Frame = frame;

	//			offset += (float)labels[i].Frame.Width + bufferSpaceBetweenLabels;
	//		}

	//		var size = new CGSize
	//		{
	//			Width = labels[0].Frame.Width + Frame.Width + bufferSpaceBetweenLabels,
	//			Height = Frame.Height
	//		};
	//		ContentSize = size;

	//		SetContentOffset(new CGPoint(0, 0), false);

	//		if (labels[0].Frame.Width > Frame.Width)
	//		{
	//			for (int i = 1; i < labels.Length; ++i)
	//			{
	//				labels[i].Hidden = false;
	//			}
	//			scroll();
	//		}
	//		else
	//		{
	//			for (int i = 1; i < labels.Length; ++i)
	//			{
	//				labels[i].Hidden = true;
	//			}
	//			CGPoint center;
	//			center = labels[0].Center;
	//			center.X = Center.X - Frame.X;
	//			labels[0].Center = center;
	//		}
	//	}

	//	/// <summary>
	//	/// 文本
	//	/// </summary>
	//	/// <value>The text.</value>
	//	public string Text
	//	{
	//		get
	//		{
	//			return labels[0].Text;
	//		}
	//		set
	//		{
	//			if (labels[0].Text == value)
	//			{
	//				return;
	//			}
	//			for (int i = 0; i < labels.Length; ++i)
	//			{
	//				labels[i].Text = value;
	//			}

	//			readjustLabels();
	//		}
	//	}

	//	/// <summary>
	//	/// 文本的颜色
	//	/// </summary>
	//	/// <value>文本的颜色</value>
	//	public UIColor TextColor
	//	{
	//		get
	//		{
	//			return labels[0].TextColor;
	//		}
	//		set
	//		{
	//			for (int i = 0; i < labels.Length; ++i)
	//			{
	//				labels[i].TextColor = value;
	//			}
	//		}
	//	}

	//	/// <summary>
	//	/// 文本的大小
	//	/// </summary>
	//	/// <value>文本的大小</value>
	//	public UIFont TextSize
	//	{
	//		get
	//		{
	//			return labels[0].Font;
	//		}
	//		set
	//		{
	//			for (int i = 0; i < labels.Length; ++i)
	//			{
	//				labels[i].Font = value;
	//			}
	//		}
	//	}
	//}
}