﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class SliderControl : EventTrigger// MonoBehaviour
{
    public Scrollbar m_Scrollbar;
    public ScrollRect m_ScrollRect;
    private float mTargetValue;
    private bool beDetected = false;
    private const float MOVE_SPEED = 1F;
    private const float SMOOTH_TIME = 0.05F;
    private float mMoveSpeed = 0f;

    public GameObject subpanel;
    private float delta = 0;//itemcell个数决定偏移值
    private float halfdelta = 0;
    //滑动响应模式所需变量
    private bool mNeedMove = false;
    private float preScrollbarValue = 0;

    public ButtonAutoSet baset;
    void Awake() {
        subpanel = transform.FindChild("Viewport/Content").gameObject;
    }

    void Start() {
        if (baset == null) {
            baset = transform.parent.FindChild("btnpaging").GetComponent<ButtonAutoSet>();            
        }
        if (m_Scrollbar == null) {
            m_Scrollbar = transform.parent.FindChild("Scrollbar").GetComponent<Scrollbar>();            
        }
        float childnum =0;
        for (int i = 0; i < subpanel.transform.childCount; i++) {
            if (subpanel.transform.GetChild(i).gameObject.activeSelf)
                childnum++;
        }
        childnum--;
       if (childnum > 0) {
           delta = 1 / childnum;
           halfdelta = delta / 2;
       }
       //Debug.Log(delta + " 1 " + halfdelta + " 1 " + childnum);
    }

    public void OnButtonClick(int value)
    {
        //if (value < 1 || (value - 1) * delta > 1) {
        //    Debug.LogError("input value exception");
        //    return;            
        //}
        if (value < 1)
        {
            Debug.LogError("input button value exception");
            return;
        }
        mTargetValue = delta * (value - 1);
        mNeedMove = true;
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        base.OnPointerDown(eventData);
        OnPointerDown();
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        base.OnPointerUp(eventData);
        OnPointerUp();
    }
    //抬起时响应
    public void OnPointerDown()
    {
        mNeedMove = false;
    }
    public void OnPointerUp()
    {
        //Debug.Log("UP-------------");
        float predelta = 0;
        float curdelta = halfdelta;
        float count = 1;//等差+2
        float targetCount = 0;//等差+1
        while (curdelta <= 1)
        {
            //Debug.Log(curdelta + " 2 " + predelta + " 2 ");
            if (m_Scrollbar.value <= curdelta && m_Scrollbar.value > predelta)
            {
                mTargetValue = delta * targetCount;
                break;
            }
            else
            {
                count += 2;
                targetCount += 1;
                predelta = curdelta;
                curdelta = halfdelta * count;
            }
        }
        if (curdelta > 1)
        {
            if (m_Scrollbar.value == 0)
                mTargetValue = 0f;
            else
                mTargetValue = 1f;
        }

        //改变按钮状态
        baset.ToggleByIndex(mTargetValue / delta);

        mNeedMove = true;
        mMoveSpeed = 0;
    }

    void Update()
    {
        if (mNeedMove)
        {
            if (Mathf.Abs(m_Scrollbar.value - mTargetValue) < 0.01f)
            {
                m_Scrollbar.value = mTargetValue;
                mNeedMove = false;
                return;
            }
            m_Scrollbar.value = Mathf.SmoothDamp(m_Scrollbar.value, mTargetValue, ref mMoveSpeed, SMOOTH_TIME);
        }
    }

    //按下后滑动响应
    //public void OnPointerDown()
    //{
    //    Debug.Log("DOWN-------------");
    //    mNeedMove = false;
    //    preScrollbarValue = m_Scrollbar.value;
    //    beDetected = true;
    //}
    //public void OnPointerUp() {
    //    Debug.Log("UP-------------");
    //    beDetected = false;
    //}

    ////检测与当前的scrollbar值的差值是否大于可偏移临界值
    //public void DetectMove() {
    //    Debug.Log(m_Scrollbar.value + "DetectMove-------------"+preScrollbarValue);
    //    float deltaValue = preScrollbarValue - m_Scrollbar.value;
    //    if (Mathf.Abs(deltaValue) > halfdelta) {
    //        if (deltaValue > 0)//要向左滚
    //        {
    //            mTargetValue = preScrollbarValue- delta;
    //        }
    //        else {
    //            mTargetValue = preScrollbarValue + delta;                
    //        }
    //        beDetected = false;
    //        mNeedMove = true;
    //        mMoveSpeed = 0;
    //    }
    //}

    
    //void Update()
    //{
    //    if (beDetected) {
    //        DetectMove();
    //    }

    //    if (mNeedMove)
    //    {
    //        if (Mathf.Abs(m_Scrollbar.value - mTargetValue) < 0.01f)
    //        {
    //            m_Scrollbar.value = mTargetValue;
    //            mNeedMove = false;
    //            return;
    //        }
    //        m_Scrollbar.value = Mathf.SmoothDamp(m_Scrollbar.value, mTargetValue, ref mMoveSpeed, SMOOTH_TIME);
    //    }
    //}
}
