package com.mjsheng.myappstore.view;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.mjsheng.myappstore.R;
import com.mjsheng.myappstore.image.ImageLoader;


/**
 * 作者： corer   时间： 2015/10/18.
 * 功能：帮助切换错误，数据为空，正在加载的页面
 * 修改：
 */
public class VaryViewHelper {
    /**
     * 切换不同视图的帮助类
     */
    public OverlapViewHelper mViewHelper;
    /**
     * 错误页面
     */
    public View mErrorView;
    /**
     * 正在加载页面
     */
    public View mLoadingView;
    /**
     * 数据为空的页面
     */
    public View mEmptyView;
    // 加载动画效果
    private Animation operatingAnim = null;
    private Context mContext;


    public VaryViewHelper(View view, Context context) {
        this(new OverlapViewHelper(view));
        this.mContext = context;
    }

    public VaryViewHelper(OverlapViewHelper helper) {
        this.mViewHelper = helper;
    }


    public void setUpEmptyView(View view, View.OnClickListener listener) {
        mEmptyView = view;
        mEmptyView.setClickable(true);
        ImageView imageView = (ImageView) mEmptyView.findViewById(R.id.iv_emptyview_layout);
        ImageLoader.getDefault().load(mContext,R.drawable.icon_nodata,imageView);
        if (imageView != null) {
            imageView.setOnClickListener(listener);
        }
    }

    public void setUpErrorView(View view, View.OnClickListener listener) {
        mErrorView = view;
        mErrorView.setClickable(true);
        ImageView imageView = (ImageView) mErrorView.findViewById(R.id.iv_errorview_layout);
        ImageLoader.getDefault().load(mContext,R.drawable.icon_nodata,imageView);
        if (imageView != null) {
            imageView.setOnClickListener(listener);
        }
    }

    public void setUpLoadingView(View view) {
        mLoadingView = view;
        mLoadingView.setClickable(true);
        ImageView imageView = (ImageView) mLoadingView.findViewById(R.id.iv_loadingview_layout);
        ImageLoader.getDefault().load(mContext,R.drawable.varyview_loadingview_bg,imageView);
    }

    public void showEmptyView() {
        cleanLoadingView();
                mViewHelper.showCaseLayout(mEmptyView);
    }

    public void showErrorView() {
        cleanLoadingView();
                mViewHelper.showCaseLayout(mErrorView);
    }

    public void cleanLoadingView(){
        if (operatingAnim != null) {
            ImageView mLoadingIv = (ImageView) mLoadingView.findViewById(R.id.iv_loadingview_layout);
            mLoadingIv.clearAnimation();
        }
    }
    public void showLoadingView() {
        ImageView mLoadingIv = (ImageView) mLoadingView.findViewById(R.id.iv_loadingview_layout);
        if (operatingAnim == null) {
            operatingAnim = AnimationUtils.loadAnimation(mContext,
                    R.anim.animition);
        }
        mLoadingIv.setAnimation(operatingAnim);
        mViewHelper.showCaseLayout(mLoadingView);
    }

    public void showDataView() {
        cleanLoadingView();
        mViewHelper.restoreLayout();
    }


    public void releaseVaryView() {
        mErrorView = null;
        mLoadingView = null;
        mEmptyView = null;
    }

    public static class Builder {
        private Context mContext;
        private View mErrorView;
        private View mLoadingView;
        private View mEmptyView;
        private View mDataView;
        private View.OnClickListener mRefreshListener;

        public Builder setContent(Context context) {
            mContext = context;
            return this;
        }
        public Builder setErrorView(View errorView) {
            mErrorView = errorView;
            return this;
        }

        public Builder setLoadingView(View loadingView) {
            mLoadingView = loadingView;
            return this;
        }

        public Builder setEmptyView(View emptyView) {
            mEmptyView = emptyView;
            return this;
        }

        public Builder setDataView(View dataView) {
            mDataView = dataView;
            return this;
        }

        public Builder setRefreshListener(View.OnClickListener refreshListener) {
            mRefreshListener = refreshListener;
            return this;
        }

        public VaryViewHelper build() {
            VaryViewHelper helper = new VaryViewHelper(mDataView,mContext);
            if (mEmptyView != null) {
                helper.setUpEmptyView(mEmptyView, mRefreshListener);
            }
            if (mErrorView != null) {
                helper.setUpErrorView(mErrorView, mRefreshListener);
            }
            if (mLoadingView != null) {
                helper.setUpLoadingView(mLoadingView);
            }
            return helper;
        }
    }

}
