package com.tenlionsoft.baselib.core.widget.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.utils.LogUtils;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

public abstract class BaseFragment extends Fragment {
    public static final int STATE_LOAD = 1234;//加载中
    public static final int STATE_ERROR = 1235;//加载失败
    public static final int STATE_SUCCESS = 1236;//加载成功
    public static final int STATE_EMPTY = 1237;//无数据
    public static final int STATE_SEARCH_EMPTY = 1238;//没有搜索到数据
    FrameLayout mFlContent;
    TextView mTvFragmentErrorHint;
    ProgressBar mPbFragmentLoading;
    ImageView mIvFragmentEmptyData;
    LinearLayout mLlHintView;
    protected LayoutInflater inflater;
    private View contentView;
    private Context context;
    private ViewGroup container;
    private View mDataView;
    protected SmartRefreshLayout mSrlView;
    protected BaseActivity mActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity().getApplicationContext();
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        this.container = container;
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_base, container, false);
        mFlContent = contentView.findViewById(R.id.fl_fragment_content);
        mTvFragmentErrorHint = contentView.findViewById(R.id.tv_fragment_error_hint);
        mPbFragmentLoading = contentView.findViewById(R.id.pb_fragment_loading);
        mIvFragmentEmptyData = contentView.findViewById(R.id.iv_fragment_empty_data);
        mLlHintView = contentView.findViewById(R.id.ll_hint_view);
        mSrlView = contentView.findViewById(R.id.srl_view);
        mSrlView.setOnRefreshListener(refreshLayout -> refreshView());
        mSrlView.setOnLoadMoreListener(refreshLayout -> loadMoreData());
        mSrlView.setEnableOverScrollBounce(false);
        mSrlView.setEnableOverScrollDrag(false);
        mDataView = LayoutInflater.from(context).inflate(getContentViewId(), container, false);
        mFlContent.addView(mDataView);
        setStateView(STATE_LOAD);
        setDataToView(mDataView);
        return contentView;
    }

    protected abstract int getContentViewId();

    protected abstract void setDataToView(View dataView);

    protected abstract void refreshView();

    protected abstract void loadMoreData();


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        contentView = null;
        container = null;
        inflater = null;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mActivity = (BaseActivity) getActivity();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    /**
     * 选择加载状态
     *
     * @param state
     */
    protected void setStateView(int state) {
        switch (state) {
            case STATE_EMPTY:
                mLlHintView.setVisibility(View.VISIBLE);
                mPbFragmentLoading.setVisibility(View.GONE);
                mIvFragmentEmptyData.setVisibility(View.VISIBLE);
                mIvFragmentEmptyData.setBackgroundResource(R.drawable.ic_empty_data);
                mTvFragmentErrorHint.setText("暂无数据");
                mDataView.setVisibility(View.GONE);
                mSrlView.setEnableLoadMore(true);
                mSrlView.setEnableRefresh(true);
                break;
            case STATE_SUCCESS:
                mLlHintView.setVisibility(View.GONE);
                mDataView.setVisibility(View.VISIBLE);
                mSrlView.setEnableLoadMore(true);
                mSrlView.setEnableRefresh(true);
                break;
            case STATE_ERROR:
                mLlHintView.setVisibility(View.VISIBLE);
                mDataView.setVisibility(View.GONE);
                mPbFragmentLoading.setVisibility(View.GONE);
                mIvFragmentEmptyData.setVisibility(View.VISIBLE);
                mIvFragmentEmptyData.setBackgroundResource(R.drawable.ic_load_error);
                mTvFragmentErrorHint.setText("加载失败,请稍后重试");
                mSrlView.setEnableLoadMore(true);
                mSrlView.setEnableRefresh(true);
                break;
            case STATE_LOAD:
                mLlHintView.setVisibility(View.VISIBLE);
                mPbFragmentLoading.setVisibility(View.VISIBLE);
                mDataView.setVisibility(View.GONE);
                mIvFragmentEmptyData.setVisibility(View.GONE);
                mTvFragmentErrorHint.setText("加载中...");
                mSrlView.setEnableLoadMore(false);
                mSrlView.setEnableRefresh(false);
                break;
            case STATE_SEARCH_EMPTY:
                mLlHintView.setVisibility(View.VISIBLE);
                mPbFragmentLoading.setVisibility(View.GONE);
                mIvFragmentEmptyData.setVisibility(View.VISIBLE);
                mIvFragmentEmptyData.setBackgroundResource(R.drawable.ic_empty_data);
                mTvFragmentErrorHint.setText("未搜索到数据");
                mDataView.setVisibility(View.GONE);
                mSrlView.setEnableLoadMore(true);
                mSrlView.setEnableRefresh(true);
                break;
            default:
                break;
        }
    }

    public Context getApplicationContext() {
        return context;
    }


    /**
     * 刷新视图
     */
    public void refreshContentView(String... data) {

    }

    public void setContentView(int layoutResID) {
        setContentView((ViewGroup) inflater.inflate(layoutResID, container, false));
    }

    public void setContentView(View view) {
        contentView = view;
    }

    public View getContentView() {
        return contentView;
    }

    public View findViewById(int id) {
        if (contentView != null)
            return contentView.findViewById(id);
        return null;
    }

}