using System;
using Android.Widget;
using Android.Content;
using Android.Views;
using Android.Util;
using Android.Runtime;

namespace Shared
{
 //滑动冲突问题后面还得完善
 /// 
 /// 位置布局
 /// 
 public class VerticalScrolViewLayout : ViewGroup
 {
 /// 
 /// 构造函数
 /// 
 public VerticalScrolViewLayout()
 {
 viewGroup = new AndroidScrolView(Application.Activity, null);
 realViewGroup = new AndroidLinearLayout(Application.Activity, this);
 (realViewGroup as AndroidLinearLayout).Orientation = Orientation.Vertical;
 viewGroup.AddView(realViewGroup);
 }


 /// 
 /// 视图宽度
 /// 
 /// The width.
 public override int Width
 {
 get
 {
 return base.Width;
 }
 set
 {
 base.Width = value;
 if (!IsCanRefresh)
 {
 return;
 }
 var layoutParameters = realViewGroup.LayoutParameters;
 layoutParameters.Width = Width;
 realViewGroup.LayoutParameters = layoutParameters;

 }
 }

 /// 
 /// 视图高度
 /// 
 /// The height.
 public override int Height
 {
 get
 {
 return base.Height;
 }
 set
 {
 base.Height = value;
 if (!IsCanRefresh)
 {
 return;
 }
 var layoutParameters = realViewGroup.LayoutParameters;
 layoutParameters.Height = Height;
 realViewGroup.LayoutParameters = layoutParameters;
 }
 }

 /// 
 /// 是否允许滑动
 /// 
 /// true if scroll enabled; otherwise, false.
 public bool ScrollEnabled
 {
 get
 {
 return (viewGroup as AndroidScrolView).ScrollEnable;
 }
 set
 {
 (viewGroup as AndroidScrolView).ScrollEnable = value;
 }
 }

 /// 
 /// 是否正在滑动
 /// 
 /// true if decelerating; otherwise, false.
 public bool Decelerating
 {
 get
 {
 return false;
 }
 } /// 
 /// 是否显示滚动条
 /// //bool _IsShowScrollBar; public bool VerticalScrollBarEnabled { get
 {
 return (viewGroup as AndroidScrolView).VerticalScrollBarEnabled;
 } set { (viewGroup as AndroidScrolView).VerticalScrollBarEnabled = value; } } 
 } /// 
 /// 竖直方向滑动控件
 /// 
 class AndroidScrolView : Android.Widget.ScrollView
 {
 /// 
 /// 是否允许滑动
 /// 
 public bool ScrollEnable = true;
 View _view;
 /// 
 /// 构造函数
 /// 
 /// Context.
 /// View.
 public AndroidScrolView(Context context, View view)
 : base(context)
 {
 _view = view;
 VerticalScrollBarEnabled = true;
 }



 public override bool DispatchTouchEvent(MotionEvent e) { //Shared.HDLUtils.WriteLine($"{GetType()} Height->{Height} Width->{Width} DispatchTouchEvent->{e.Action}"); if (e.Action == MotionEventActions.Down) {
 //还原允许拦截事件 RequestDisallowInterceptTouchEvent(false);
 } return base.DispatchTouchEvent(e); } float mLastX, mLastY ;
 public override bool OnInterceptTouchEvent(MotionEvent ev)
 {
 //Shared.HDLUtils.WriteLine($"{GetType()} Height->{Height} Width->{Width} OnInterceptTouchEvent->{ev.Action}"); if (!ScrollEnable || disallowIntercept)
 {
 return false;
 } switch (ev.Action) {
 case MotionEventActions.Down:
 mLastX = ev.RawX;
 mLastY = ev.RawY;
 Parent?.RequestDisallowInterceptTouchEvent(true);
 break;
 case MotionEventActions.Move:
 var x = ev.RawX;
 var y = ev.RawY;
 var deltaX = x - mLastX;
 var deltaY = y - mLastY;

 if (Math.Abs(deltaX) < Math.Abs(deltaY)) { //向下滑动,如果当前已经滑动到顶,就允许父控件的拦截事件 if (deltaY > 0 && ScrollY == 0) { Parent?.RequestDisallowInterceptTouchEvent(false); return false; } //向上滑动时,如果translateY等于高度时,即表明滑动到底部,可又顶层View控制滑动 var mChildH = 0; if (0 < ChildCount) { mChildH = GetChildAt(ChildCount - 1).Bottom; } var translateY = mChildH - ScrollY; if (deltaY < 0 && translateY == Height) { Parent?.RequestDisallowInterceptTouchEvent(false); return false; } } else {
 //左右滑动时,允许父控件的拦截 if (4 < Math.Abs(deltaX)) { Parent?.RequestDisallowInterceptTouchEvent(false); }
 }
 break;
 }

 return base.OnInterceptTouchEvent(ev);
 }
 /// 
 /// 重写点击事件
 /// 
 /// true, if touch event was oned, false otherwise.
 /// E.
 public override bool OnTouchEvent(MotionEvent e)
 {
 //Shared.HDLUtils.WriteLine($"{GetType()} Height->{Height} Width->{Width} OnInterceptTouchEvent->{e.Action}");
 _view?.TouchEvent(e);
 base.OnTouchEvent(e);
 //如果返回真父控件就不需要处理了
 return true;
 }
 bool disallowIntercept;
 public override void RequestDisallowInterceptTouchEvent(bool disallowIntercept)
 {
 this.disallowIntercept = disallowIntercept;
 //Shared.HDLUtils.WriteLine($"{GetType()} Height->{Height} RequestDisallowInterceptTouchEvent->{disallowIntercept}");
 base.RequestDisallowInterceptTouchEvent(disallowIntercept);
 Parent?.RequestDisallowInterceptTouchEvent(disallowIntercept);
 }
 }
}