package kr.co.hanwha.hifive.view.refresh;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;

/**
 * Created by sanghoonkwon on 2018. 1. 12..
 */

public class HeaderView extends FrameLayout implements HeaderListener {

    private final ImageView mHeaderImageView;
    private final TextView mTextView;

    public HeaderView(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.item_refresh_header, this, true);
        mHeaderImageView = (ImageView) findViewById(R.id.refresh_imageView);
        mTextView = (TextView) findViewById(R.id.textView);
    }

    /**
     * @param scrollY
     */
    @Override
    public void onRefreshBefore(int scrollY, int headerHeight) {
        mHeaderImageView.setImageResource(R.mipmap.temp_ic_rabbit);
        mTextView.setText("더 쓱- 내려봐요");

        //mRefreshTv.setText("pull to refresh");
        /*Log.e("TAG", "onRefreshBefore: " + scrollY);
        if (Math.abs(scrollY) < headerHeight * 0.5f) {
            return;
        }
        float v = Math.abs(Math.abs(scrollY + headerHeight * 0.5f) * 1.0f / headerHeight);
        if (v > 1) {
            v = 1;
        }
        int current = (int) (v * 44);
        Log.e("TAG", "frame: " + current);*/
        //mHeaderImageView.setBackgroundDrawable(mFrameAnimation.getFrame(current));
    }

    /**
     * @param scrollY
     */
    @Override
    public void onRefreshAfter(int scrollY, int headerHeight) {
        mHeaderImageView.setImageResource(R.mipmap.temp_ic_rabbit);
        mTextView.setText("손을 놓으면 업데이트가 쓱-");

        //mHeaderImageView.setBackgroundDrawable(mFrameAnimation.getFrame(mNumberOfFrames - 1));
        //mRefreshTv.setText("release");

    }

    /**
     * @param scrollY
     */
    @Override
    public void onRefreshReady(int scrollY, int headerHeight) {
    }

    /**
     * @param scrollY
     */
    @Override
    public void onRefreshing(int scrollY, int headerHeight) {
        //mRefreshTv.setText("onRefreshing");
        //mHeaderImageView.setBackgroundDrawable(mFrameAnimation);
        //((AnimationDrawable) mHeaderImageView.getBackground()).start();
    }

    /**
     * @param scrollY
     * @param isRefreshSuccess
     */
    @Override
    public void onRefreshComplete(int scrollY, int headerHeight, boolean isRefreshSuccess) {
        //mRefreshTv.setText(isRefreshSuccess ? "refresh complete" : "refresh fail");
        /*Drawable background = mHeaderImageView.getBackground();
        if (background instanceof AnimationDrawable) {
            ((AnimationDrawable) background).stop();
        }*/
    }

    /**
     * @param scrollY
     */
    @Override
    public void onRefreshCancel(int scrollY, int headerHeight) {
        //mRefreshTv.setText("onRefreshCancel");
    }

    @Override
    public int getRefreshHeight() {
        return getPixelFromDimension(getContext(), R.dimen.refresh_header_height);
    }

    public static int getPixelFromDimension(Context context, int dimenRes) {
        return context.getResources().getDimensionPixelOffset(dimenRes);
    }
}
