package net.heartshield.prevent;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.BoolRes;
import android.support.annotation.Nullable;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import net.heartshield.data.AppInfo;
import net.heartshield.data.DataManagerFactory;
import net.heartshield.data.IMeasurementDataManager;
import net.heartshield.data.MeasurementDataManager;

/**
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-05-24
 */
public class DisclaimerActivity extends Activity {
    public static final String TAG = "DisclaimerActivity";

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

    @Override
    protected void onResume() {
        super.onResume();

        TextView activationText = (TextView) findViewById(R.id.activationText);
        activationText.setMovementMethod(LinkMovementMethod.getInstance());

        boolean forceShow = getIntent().getBooleanExtra("forceShow", false);
        AppInfo appInfo = AppInfo.getInstance(this);

        boolean isDebugBuild = BuildConfig.BUILD_TYPE.equals("debug");
        boolean show = (appInfo.isFirstInstall() || !appInfo.isActivated() || forceShow) && !isDebugBuild;

        Log.i(TAG, "appInfo.isFirstInstall()=" + Boolean.toString(appInfo.isFirstInstall()) + " || !appInfo.isActivated()=" + Boolean.toString(!appInfo.isActivated()) + " || forceShow=" + Boolean.toString(forceShow) + ";");

        View activation = findViewById(R.id.activation);
        activation.setVisibility(appInfo.isActivated() ? View.GONE : View.VISIBLE);

        if(isDebugBuild && appInfo.getAppMeta().appId.equals("")) {
            // apparently happens at reinstall

            // activate using a dummy ID
            appInfo.activate("DEADBEEF");
            // immediately show home screen
            showHome();
            return;
        }

        if(!appInfo.isFirstInstall() && !appInfo.isActivated()) {
            selfActivate(appInfo.getAppMeta().appId);
            return;
        }

        if(!show)
            showHome();
    }

    private void selfActivate(String activationCode) {
        // assume that user got an update which now forces activation - activate them with their existing appId
        EditText activationCodeTxt = (EditText) findViewById(R.id.activationCode);
        activationCodeTxt.setText(activationCode);

        // send activation to server (goes to background)
        accept(null);

        // immediately show home screen
        showHome();
    }

    public void accept(View view) {
        final AppInfo appInfo = AppInfo.getInstance(this);

        if(!appInfo.isActivated()) {
            EditText activationCodeTxt = (EditText) findViewById(R.id.activationCode);
            String activationCode = activationCodeTxt.getText().toString().toUpperCase();
            if(activationCode.equals("")) {
                Toast.makeText(DisclaimerActivity.this, "Need an activation code to proceed.", Toast.LENGTH_LONG).show();
                return;
            }

            IMeasurementDataManager mdm = DataManagerFactory.getInstance().getDataManager(getApplicationContext());
            mdm.activate(activationCode, new IMeasurementDataManager.ActivationResultListener() {
                @Override
                public void onResult(String appId) {
                    appInfo.activate(appId);
                    Toast.makeText(DisclaimerActivity.this, "Activation successful.", Toast.LENGTH_LONG).show();

                    showHome();
                }

                @Override
                public void onError(String reason) {
                    Toast.makeText(DisclaimerActivity.this, "Activation failed: " + reason, Toast.LENGTH_LONG).show();
                }
            });
        } else {
            showHome();
        }
    }

    private void showHome() {
        Intent intent = new Intent(DisclaimerActivity.this, HomeActivity.class);
        startActivity(intent);
    }

    public void exit(View view) {
        moveTaskToBack(true);
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(1);
    }

    public void licenses(View view) {
        Intent intent = new Intent(DisclaimerActivity.this, LicenseInfoActivity.class);
        startActivity(intent);
    }
}
