package com.yuqirong.meituanlistview.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.yuqirong.meituanlistview.R;

/**
 * Created by Anyway on 2016/4/17.
 */
public class MeiTuanListView extends ListView {

    // 普通状态模式
    private static final int NONE_MODE = 0;
    // 下拉刷新模式
    private static final int PULL_DOWN_MODE = 1;
    // 可松开刷新模式
    private static final int RELEASE_REFRESH_MODE = 2;
    // 刷新中模式
    private static final int REFRESHING_MODE = 3;
    // 阻值
    private static final int RATIO = 3;

    private static final String TAG = "MeiTuanListView";
    // 模式
    private int mode = NONE_MODE;
    // 下拉刷新头部
    private View headerRefreshView;
    // 头部高度
    private int refreshViewHeight;
    // 手指按下的Y值
    private float downY;
    // 刷新监听器
    private OnRefreshListener listener;
    //
    private MeiTuanOvalView mtOvalView;
    // 帧动画的ImageView
    private ImageView aniImageView;
    // 动画是否已经开始
    private boolean isStart;
    // 只有在listview顶端时下拉才可以刷新
    private boolean canRefresh;

    public MeiTuanListView(Context context) {
        this(context, null);
    }

    public MeiTuanListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MeiTuanListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        setOverScrollMode(View.OVER_SCROLL_NEVER);
        headerRefreshView = LayoutInflater.from(getContext()).inflate(R.layout.layout_header, null);
        mtOvalView = (MeiTuanOvalView) headerRefreshView.findViewById(R.id.mtFirstView);
        aniImageView = (ImageView) headerRefreshView.findViewById(R.id.iv_anim);
        // 去测量
        headerRefreshView.measure(0, 0);
        refreshViewHeight = headerRefreshView.getMeasuredHeight();
        headerRefreshView.setPadding(0, -refreshViewHeight, 0, 0);
        addHeaderView(headerRefreshView, null, false);
    }

    public interface OnRefreshListener {
        void refreshing();
    }

    public void setOnRefreshListener(OnRefreshListener listener) {
        this.listener = listener;
    }

    /**
     * 刷新完成后调用
     */
    public void notifyRefreshFinished() {
        mode = NONE_MODE;
        headerRefreshView.setPadding(0, -refreshViewHeight, 0, 0);
        aniImageView.setVisibility(GONE);
        mtOvalView.setVisibility(VISIBLE);
        isStart = false;
        canRefresh = false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 在listview顶端时才可下拉刷新
                if (getFirstVisiblePosition() == 0) {
                    Log.i(TAG, "canRefresh : " + canRefresh);
                    downY = ev.getY();
                    canRefresh = true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode != REFRESHING_MODE && canRefresh) {
                    float moveY = ev.getY();
                    float offsetY = moveY - downY;
                    headerRefreshView.setPadding(0, (int) (headerRefreshView.getPaddingTop() + offsetY / RATIO), 0, 0);
                    if (headerRefreshView.getPaddingTop() <= 0) {
                        mode = PULL_DOWN_MODE;
                        mtOvalView.setVisibility(VISIBLE);
                        aniImageView.setVisibility(GONE);
                        mtOvalView.setScale((refreshViewHeight + headerRefreshView.getPaddingTop()) * 1f / refreshViewHeight);
                        isStart = false;
                    } else {
                        mode = RELEASE_REFRESH_MODE;
                        if (!isStart) {
                            mtOvalView.setVisibility(GONE);
                            aniImageView.setVisibility(VISIBLE);
                            aniImageView.setImageResource(R.drawable.pull_end_imag_frame);
                            AnimationDrawable animationDrawable = (AnimationDrawable) aniImageView.getDrawable();
                            // 只执行一次动画
                            animationDrawable.setOneShot(true);
                            animationDrawable.start();
                            isStart = true;
                        }
                    }
                    downY = moveY;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (mode != REFRESHING_MODE && canRefresh) {
                    if (headerRefreshView.getPaddingTop() > 0) {
                        mode = REFRESHING_MODE;
                        headerRefreshView.setPadding(0, 0, 0, 0);
                        aniImageView.setImageResource(R.drawable.refreshing_image_frame);
                        AnimationDrawable animationDrawable = (AnimationDrawable) aniImageView.getDrawable();
                        animationDrawable.start();
                        if (listener != null) {
                            listener.refreshing();
                        }
                    } else {
                        mode = NONE_MODE;
                        headerRefreshView.setPadding(0, -refreshViewHeight, 0, 0);
                        mtOvalView.setScale(0f);
                        isStart = false;
                        canRefresh = false;
                    }
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

}
