package it.integry.integrywmsnative;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.SpannableString;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import it.integry.integrywmsnative.core.REST.watcher.ServerStatusChecker;
import it.integry.integrywmsnative.core.expansion.RunnableArgss;
import it.integry.integrywmsnative.core.expansion.RunnableArgsss;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.core.utility.PermissionsHelper;
import it.integry.integrywmsnative.gest.login.LoginActivity;
import it.integry.integrywmsnative.view.dialogs.DialogSimpleMessageHelper;

public class SplashActivity extends AppCompatActivity {

    @BindView(R.id.app_version_textview)
    TextView appVersionTextView;

    private RunnableArgsss<Integer, String[], List<Integer>> onRequestPermissionResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        ButterKnife.bind(this);

        initAppVersion();

        initPermissions(this::init);


    }

    private void initPermissions(Runnable onComplete) {
        PermissionsHelper.askPermissions(this, () -> {
            onComplete.run();
        }, permanentlyDenied -> {
            if(permanentlyDenied) {
                DialogSimpleMessageHelper.makeErrorDialog(this,
                        new SpannableString(getText(R.string.permissions_permanently_denied)), null, () -> {
                            this.finish();
                        }).show();
            } else {
                initPermissions(onComplete);
            }
        });
    }


    private void initAppVersion() {
        try {
            PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            String version = pInfo.versionName;
            appVersionTextView.setText("v" + version);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void init() {
        if(SettingsManager.i().user.username == null && SettingsManager.i().user.password == null){
            startLoginActivity();
        } else {
            initFirstData(this::startMainActivity);
        }
    }

    private void initFirstData(Runnable onComplete) {

        SettingsManager.reloadDBVariables(onComplete, ex -> {
            DialogSimpleMessageHelper.makeErrorDialog(this,
                    new SpannableString(ex.getMessage()), null, this::finish).show();
            }
        );
    }




    public void setOnRequestPermissionsResult(RunnableArgsss<Integer, String[], List<Integer>> onRequestPermissionResult) {
        this.onRequestPermissionResult = onRequestPermissionResult;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        if(this.onRequestPermissionResult != null) {
            List<Integer> grantResultsList = new ArrayList<>();
            for(int i = 0; i < grantResults.length; i++) {
                grantResultsList.add(grantResults[i]);
            }

            this.onRequestPermissionResult.run(requestCode, permissions, grantResultsList);
        }
    }





    private void startLoginActivity(){
        this.finish();
        Intent myIntent = new Intent(this, LoginActivity.class);
        startActivity(myIntent);
    }

    private void startMainActivity(){
        this.finish();
        Intent myIntent = new Intent(this, MainActivity.class);
        startActivity(myIntent);
    }


}
