package com.tofly.latte_core.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.trello.rxlifecycle2.components.support.RxFragment;

import butterknife.ButterKnife;

public abstract class BaseDelegate<P extends IPresenter> extends RxFragment implements BaseView {


    protected CollapsingToolbarLayoutState state;
    private ProgressDialog dialog;
    private long lastClickTime = 0;

    public enum CollapsingToolbarLayoutState {
        EXPANDED,
        COLLAPSED,
        INTERNEDIATE
    }

    private View rootView = null;

    protected P mPresenter;

    protected abstract P createPresenter();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (setLayout() instanceof Integer) {
            rootView = inflater.inflate((Integer) setLayout(), container, false);
        } else if (setLayout() instanceof View) {
            rootView = (View) setLayout();
        }
        dialog = new ProgressDialog(getContext());
        dialog.setCancelable(false);
        ButterKnife.bind(this, rootView);
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.onMvpAttachView(this, savedInstanceState);
        }
        initView();
        initNet();
        return rootView;
    }


    @Override
    public void onEmpty() {
        onNetEmpty();
    }

    @Override
    public void onError(String message) {
        onNetError();
    }

    @Override
    public void showLoading(String msg) {
        if (dialog != null) {
            if (!dialog.isShowing()) {
                dialog.setMessage(msg);
                dialog.show();
            }
        }
    }

    @Override
    public void hideLoading() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.hide();
            }
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onMvpDetachView(false);
            mPresenter.onMvpDestroy();
        }
    }

    public abstract Object setLayout();

    public abstract void initNet();

    public abstract void initView();

    protected void onNetError() {

    }

    protected void onNetEmpty() {

    }

    @Override
    public void onPause() {
        super.onPause();
        if (mPresenter != null) {
            mPresenter.onMvpPause();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mPresenter != null) {
            mPresenter.onMvpStop();
        }
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mPresenter != null) {
            mPresenter.onMvpSaveInstanceState(outState);
        }
    }

    public boolean isOverTime() {
        boolean isSuccess = false;
        long currentTime = System.currentTimeMillis();
        long mClickTime = 1000;
        if (currentTime - lastClickTime > mClickTime) {
            lastClickTime = currentTime;
            isSuccess = true;
        }
        return isSuccess;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
