package de.beyondsoft.ownchat.utils.ui;

import de.beyondsoft.ownchat.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Custom layout to show loading, try again and error messages.
 * <p>
 * Created by Rares on 20/09/16.
 */
public class EmptyLayout extends FrameLayout {

    @BindView(R.id.empty_layout_progress_bar)
    ProgressBar mProgressBar;

    @BindView(R.id.empty_layout_try_again_layout)
    View mTryAgainLayout;

    @BindView(R.id.empty_layout_error_text)
    TextView mErrorText;

    @BindView(R.id.empty_layout_empty_text)
    TextView mEmptyText;

    @BindView(R.id.empty_layout_try_again_button)
    TextView mRetryButton;

    private OnClickListener mOnTryAgainClickListener;

    public EmptyLayout(Context context) {
        super(context);
    }

    public EmptyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public EmptyLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        inflate(getContext(), R.layout.empty_layout, this);
        ButterKnife.bind(this);

        ViewUtils.setProgressBarColorToOrange(mProgressBar);

        TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.EmptyLayout, 0, 0);

        try {
            final CharSequence errorText = a.getText(R.styleable.EmptyLayout_emptyLayoutErrorText);
            if (errorText != null) {
                mErrorText.setText(errorText);
            }
            final int errorTextMaxWith = a.getDimensionPixelSize(R.styleable.EmptyLayout_emptyLayoutErrorTextMaxWidth, 0);
            if (errorTextMaxWith != 0) {
                mErrorText.setMaxWidth(errorTextMaxWith);
            }
        } finally {
            a.recycle();
        }
    }

    @OnClick(R.id.empty_layout_try_again_button)
    void tryAgain(View view) {
        if (mOnTryAgainClickListener != null) {
            mOnTryAgainClickListener.onClick(view);
        }
    }

    public void showLoading() {
        setVisibility(VISIBLE);
        mProgressBar.setVisibility(VISIBLE);
        mTryAgainLayout.setVisibility(GONE);
        mEmptyText.setVisibility(GONE);
    }

    public void showError() {
        setVisibility(VISIBLE);
        mProgressBar.setVisibility(GONE);
        mTryAgainLayout.setVisibility(VISIBLE);
        mEmptyText.setVisibility(GONE);
    }

    public void setErrorText(String errorText) {
        mErrorText.setText(errorText);
    }

    public void setOnTryAgainClickListener(OnClickListener onTryAgainClickListener) {
        mOnTryAgainClickListener = onTryAgainClickListener;
    }

    public void setEmptyText(String text) {
        mEmptyText.setText(text);
    }

    public void hideRetryButton() {
        mRetryButton.setVisibility(GONE);
    }

    public void showEmpty() {
        setVisibility(VISIBLE);
        mProgressBar.setVisibility(GONE);
        mTryAgainLayout.setVisibility(GONE);
        mEmptyText.setVisibility(VISIBLE);
    }
}
