package com.atmob.elec_asst.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewbinding.ViewBinding;
import com.gyf.immersionbar.ImmersionBar;
import com.atmob.elec_asst.R;
import com.atmob.elec_asst.utils.SizeUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.List;

public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity {

    protected T binding;

    private ViewModelProvider viewModelProvider;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBinding();
        initImmersion();
        initViewModelProvider();
        initViewModel();
    }

    private void initBinding() {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        if (type == null) {
            return;
        }
        Class<? extends ViewBinding> bindingClass = (Class<? extends ViewBinding>) type.getActualTypeArguments()[0];
        try {
            Method inflate = bindingClass.getMethod("inflate", LayoutInflater.class);
            binding = (T) inflate.invoke(null, getLayoutInflater());
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        ViewDataBinding viewDataBinding = binding instanceof ViewDataBinding ? ((ViewDataBinding) binding) : null;
        if (viewDataBinding != null) {
            viewDataBinding.setLifecycleOwner(this);
        }
        if (binding != null) {
            setContentView(binding.getRoot());
        }
    }

    private void initImmersion() {
        if (shouldImmersion()) {
            ImmersionBar immersionBar = ImmersionBar.with(this);
            configImmersion(immersionBar);
            immersionBar.init();
        }
    }

    protected abstract boolean shouldImmersion();

    protected void configImmersion(@NonNull ImmersionBar immersionBar) {

    }

    private void initViewModelProvider() {
            viewModelProvider = new ViewModelProvider(this);
    }

    protected final ViewModelProvider getViewModelProvider() {
        return viewModelProvider;
    }

    protected void initViewModel() {

    }

    protected void addTopStatusBarHeight(ViewGroup target) {
        ViewGroup.MarginLayoutParams marginLayoutParams
                = (ViewGroup.MarginLayoutParams) target.getLayoutParams();
        marginLayoutParams.topMargin = SizeUtil.getStatusBarHeight(this);
        target.setLayoutParams(marginLayoutParams);
    }

    @Override
    public void onBackPressed() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        List<Fragment> fragments = supportFragmentManager.getFragments();
        for (Fragment fragment : fragments) {
            IFragmentBackController iFragmentBackController = fragment instanceof IFragmentBackController ? ((IFragmentBackController) fragment) : null;
            if (iFragmentBackController != null) {
                if (!iFragmentBackController.canBack()) {
                    onFragmentBackRefused();
                    return;
                }
            }
        }
        super.onBackPressed();
        overridePendingTransition(R.anim.anim_alpha_in, R.anim.anim_alpha_out);
    }

    protected void onFragmentBackRefused() {

    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.anim_alpha_in, R.anim.anim_alpha_out);
    }

    @Override
    public void startActivity(Intent intent, @Nullable Bundle options) {
        super.startActivity(intent, options);
        overridePendingTransition(R.anim.anim_alpha_in, R.anim.anim_alpha_out);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.anim_alpha_in, R.anim.anim_alpha_out);
    }
}