package ch.pizzalemon.android.activity;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import ch.pizzalemon.android.R;
import ch.pizzalemon.android.helper.DisplayHelper;
import io.github.inflationx.viewpump.ViewPumpContextWrapper;

/**
 * Created by cimenmus on 11/09/2017.
 */

public class BaseActivity extends AppCompatActivity {

    public static BaseActivity currentActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCurrentActivity(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        if (layoutResID != R.layout.activity_main){
            applyToInsentForNavigationBar();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setCurrentActivity(this);
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
    }

    public static void setCurrentActivity(BaseActivity currentActivity) {
        BaseActivity.currentActivity = currentActivity;
    }

    public int getAppVersionCode(){
        PackageInfo pInfo = null;
        try {
            pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
        return pInfo.versionCode;
    }

    public String getAppVersionName(){
        PackageInfo pInfo = null;
        try {
            pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
        return pInfo.versionName;
    }

    private void applyToInsentForNavigationBar() {
        if (DisplayHelper.getNavMode() != NavMode.THREE_BUTTON){
            return;
        }
        ViewGroup content = findViewById(android.R.id.content);
        if (content.getChildCount() > 0) {
            View rootView = content.getChildAt(0);

            ViewCompat.setOnApplyWindowInsetsListener(rootView, (v, insets) -> {
                Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                v.setPadding(0, 0, 0, systemBars.bottom);
                return insets;
            });

            ViewCompat.requestApplyInsets(rootView);
        }
    }

    private void applyToInsentForStatusBar() {
        ViewGroup content = findViewById(android.R.id.content);
        if (content.getChildCount() > 0) {
            View rootView = content.getChildAt(0);

            ViewCompat.setOnApplyWindowInsetsListener(rootView, (v, insets) -> {
                Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
                v.setPadding(0, systemBars.top, 0, 0);
                return insets;
            });

            ViewCompat.requestApplyInsets(rootView);
        }
    }
}