package com.ifavine.pay.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ifavine.pay.R;
import com.ifavine.pay.utils.NetworkUtils;


/**
 * Created by tree on 7/5/2016.
 */
public class ErrorLayout extends LinearLayoutCompat
        implements View.OnClickListener {// , ISkinUIObserver {

    public static final int HIDE_LAYOUT = 4;
    public static final int NETWORK_ERROR = 1;
    public static final int NETWORK_LOADING = 2;
    public static final int NODATA_ENABLE_CLICK = 5;
    public static final int NO_LOGIN = 6;
    public static final int NODATA_CLOUD = 7;
    public static final int NODATA_FAVOURITE = 8;
    public static final int NODATA_HISTORY = 9;
    public static final int NO_PERMISSION = 10;
    public static final int NO_MSG = 11;

    private ProgressBar animProgress;
    private boolean clickEnable = true;
    private final Context context;
    public ImageView img;
    private OnClickListener listener;
    private int mErrorState;
    private RelativeLayout mLayout;
    private String strNoDataContent = "";
    private TextView tv;
    private Button btn;

    public ErrorLayout(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public ErrorLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        View view = View.inflate(context, R.layout.view_error_layout, null);
        img = (ImageView) view.findViewById(R.id.img_error_layout);
        tv = (TextView) view.findViewById(R.id.tv_error_layout);
        btn = (Button) view.findViewById(R.id.btn_error_layout);
        mLayout = (RelativeLayout) view.findViewById(R.id.pageerrLayout);
        animProgress = (ProgressBar) view.findViewById(R.id.animProgress);
        setBackgroundColor(-1);
        setOnClickListener(this);
        img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clickEnable) {
                    // setErrorType(NETWORK_LOADING);
                    if (listener != null) listener.onClick(v);
                }
            }
        });
        addView(view);
        changeErrorLayoutBgMode(context);
    }

    public void changeErrorLayoutBgMode(Context context1) {
        // mLayout.setBackgroundColor(SkinsUtil.getColor(context1,
        // "bgcolor01"));
        // tv.setTextColor(SkinsUtil.getColor(context1, "textcolor05"));
    }

    public void dismiss() {
        mErrorState = HIDE_LAYOUT;
        setVisibility(View.GONE);
    }

    public int getErrorState() {
        return mErrorState;
    }

    public boolean isLoadError() {
        return mErrorState == NETWORK_ERROR;
    }

    public boolean isLoading() {
        return mErrorState == NETWORK_LOADING;
    }

    @Override
    public void onClick(View v) {
        if (clickEnable) {
            // setErrorType(NETWORK_LOADING);
            if (listener != null) listener.onClick(v);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        // MyApplication.getInstance().getAtSkinObserable().registered(this);
        onSkinChanged();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        // MyApplication.getInstance().getAtSkinObserable().unregistered(this);
    }

    public void onSkinChanged() {
        // mLayout.setBackgroundColor(SkinsUtil
        // .getColor(getContext(), "bgcolor01"));
        // tv.setTextColor(SkinsUtil.getColor(getContext(), "textcolor05"));
    }

    public void setDayNight(boolean flag) {
    }

    public void setErrorMessage(String msg) {
        tv.setText(msg);
    }

    /**
     * 新添设置背景
     *
     * @author 火蚁 2015-1-27 下午2:14:00
     */
    public void setErrorImag(int imgResource) {
        try {
            img.setImageResource(imgResource);
        } catch (Exception e) {
        }
    }

    public void setErrorType(int state) {
        setVisibility(View.VISIBLE);
        switch (state) {
            case NETWORK_ERROR:
                mErrorState = NETWORK_ERROR;
                // img.setBackgroundDrawable(SkinsUtil.getDrawable(context,"ic_page_failed"));
                if (NetworkUtils.isNetAvailable(getContext())) {
                    tv.setText(R.string.error_view_load_error_click_to_refresh);
                    img.setBackgroundResource(R.mipmap.icon_data_error);
                } else {
                    tv.setText(R.string.error_view_network_error_click_to_refresh);
                    img.setBackgroundResource(R.mipmap.icon_network_error);
                    btn.setText(R.string.error_view_start);
                    btn.setVisibility(VISIBLE);
                    btn.setOnClickListener(listener);
                }
                img.setVisibility(View.VISIBLE);
                animProgress.setVisibility(View.GONE);
                clickEnable = true;
                break;
            case NETWORK_LOADING:
                mErrorState = NETWORK_LOADING;
                // animProgress.setBackgroundDrawable(SkinsUtil.getDrawable(context,"loadingpage_bg"));
                animProgress.setVisibility(View.VISIBLE);
                img.setVisibility(View.GONE);
                tv.setText(R.string.error_view_loading);
                clickEnable = false;
                break;
            case NODATA_ENABLE_CLICK:
                mErrorState = NODATA_ENABLE_CLICK;
                // animProgress.setBackgroundDrawable(SkinsUtil.getDrawable(context,"loadingpage_bg"));
                animProgress.setVisibility(View.GONE);
                img.setVisibility(View.VISIBLE);
                img.setBackgroundResource(R.mipmap.icon_data_error);
                tv.setText(R.string.error_view_empty_layout_label);
                break;

            case NO_PERMISSION:
                mErrorState = NODATA_ENABLE_CLICK;
                animProgress.setVisibility(View.GONE);
                img.setVisibility(View.VISIBLE);
                img.setBackgroundResource(R.mipmap.icon_data_error);
                tv.setText(R.string.error_view_no_permission);
                break;

            case HIDE_LAYOUT:
                setVisibility(View.GONE);
                break;

            case NODATA_CLOUD:
                animProgress.setVisibility(View.GONE);
                img.setBackgroundResource(R.mipmap.icon_data_error);
                img.setVisibility(View.VISIBLE);
                tv.setText(R.string.error_view_empty_layout_label);
                tv.setVisibility(View.VISIBLE);
                clickEnable = true;
                break;
            case NO_MSG:
                animProgress.setVisibility(View.GONE);
                img.setBackgroundResource(R.mipmap.icon_data_error);
                img.setVisibility(View.VISIBLE);
                tv.setText(R.string.error_view_empty_msg);
                tv.setVisibility(View.VISIBLE);
                clickEnable = true;
                break;

            default:
                break;
        }
    }

    public void setNoDataContent(String noDataContent) {
        strNoDataContent = noDataContent;
    }

    public void setOnLayoutClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void setVisibility(int visibility) {
        if (visibility == View.GONE) mErrorState = HIDE_LAYOUT;
        super.setVisibility(visibility);
    }
}
