package com.cooleshow.base.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cooleshow.base.R;
import com.cooleshow.base.common.AppManager;
import com.cooleshow.base.data.net.ApiException;
import com.cooleshow.base.presenter.view.BaseView;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.base.widgets.ProgressLoading;
import com.trello.rxlifecycle4.components.support.RxAppCompatActivity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.viewbinding.ViewBinding;

/**
 * Author by pq, Date on 2022/4/27.
 */
public abstract class BaseActivity<V extends ViewBinding> extends RxAppCompatActivity implements BaseView {
    protected V viewBinding = null;
    protected ProgressLoading mLoading;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = getLayoutView();
        setContentView(viewBinding.getRoot());
        AppManager.Companion.getInstance().addActivity(this);
        QMUIStatusBarHelper.translucent(this);
        initView();
        initData();
    }

    protected void initData() {
        mLoading = ProgressLoading.Companion.create(this);
    }

    public void initMidTitleToolBar(Toolbar toolbar, String title) {
        if (toolbar == null) {
            return;
        }
        TextView tv_title = toolbar.findViewById(R.id.title);
        ImageView ivBack = toolbar.findViewById(R.id.iv_back);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        toolbar.setTitle("");
//        ivBack.setImageResource(R.drawable.icon_back_black);
//        tv_title.setTextColor(getResources().getColor(R.color.common_black));
        tv_title.setText(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
    }


    protected abstract void initView();

    protected abstract V getLayoutView();

    @Override
    public void showLoading() {
        if (mLoading != null) {
            mLoading.showLoading();
        }
    }

    @Override
    public void hideLoading() {
        if (mLoading != null) {
            mLoading.hideLoading();
        }
    }

    @Override
    public void onError(@NonNull Throwable throwable) {
        if (throwable instanceof ApiException) {
            ApiException apiException = (ApiException) throwable;
            ToastUtils.showShort("onError:" + apiException.getErrmsg());
        }
    }

    public V getViewBinding() {
        return viewBinding;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.Companion.getInstance().finishActivity(this);
        if (mLoading != null) {
            mLoading.stopAnim();
        }
        if (viewBinding != null) {
            viewBinding = null;
        }
    }
}
