package info.nightscout.androidaps.plugins.pump.insight.activities;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import javax.inject.Inject;

import dagger.android.support.DaggerAppCompatActivity;
import info.nightscout.androidaps.insight.R;
import info.nightscout.androidaps.plugins.pump.insight.InsightAlertService;
import info.nightscout.androidaps.plugins.pump.insight.descriptors.Alert;
import info.nightscout.androidaps.plugins.pump.insight.descriptors.AlertStatus;
import info.nightscout.androidaps.plugins.pump.insight.utils.AlertUtils;
import info.nightscout.androidaps.utils.HtmlHelper;

public class InsightAlertActivity extends DaggerAppCompatActivity {

    @Inject AlertUtils alertUtils;

    private InsightAlertService alertService;

    private ImageView icon;
    private TextView errorCode;
    private TextView errorTitle;
    private TextView errorDescription;
    private Button mute;
    private Button confirm;

    private final ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            alertService = ((InsightAlertService.LocalBinder) binder).getService();
            alertService.getAlertLiveData().observe(InsightAlertActivity.this, alert -> {
                if (alert == null) finish();
                else update(alert);
            });
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            alertService = null;
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insight_alert);

        bindService(new Intent(this, InsightAlertService.class), serviceConnection, BIND_AUTO_CREATE);

        icon = findViewById(R.id.icon);
        errorCode = findViewById(R.id.error_code);
        errorTitle = findViewById(R.id.error_title);
        errorDescription = findViewById(R.id.error_description);
        mute = findViewById(R.id.mute);
        confirm = findViewById(R.id.confirm);

        setFinishOnTouchOutside(false);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    }

    @Override
    protected void onDestroy() {
        unbindService(serviceConnection);
        super.onDestroy();
    }

    public void update(Alert alert) {
        mute.setEnabled(true);
        mute.setVisibility(alert.getAlertStatus() == AlertStatus.SNOOZED ? View.GONE : View.VISIBLE);
        confirm.setEnabled(true);
        this.icon.setImageDrawable(ContextCompat.getDrawable(this, alertUtils.getAlertIcon(alert.getAlertCategory())));
        this.errorCode.setText(alertUtils.getAlertCode(alert.getAlertType()));
        this.errorTitle.setText(alertUtils.getAlertTitle(alert.getAlertType()));
        String description = alertUtils.getAlertDescription(alert);
        if (description == null) this.errorDescription.setVisibility(View.GONE);
        else {
            this.errorDescription.setVisibility(View.VISIBLE);
            this.errorDescription.setText(HtmlHelper.INSTANCE.fromHtml(description));
        }
    }

    public void muteClicked(View view) {
        mute.setEnabled(false);
        alertService.mute();
    }

    public void confirmClicked(View view) {
        mute.setEnabled(false);
        confirm.setEnabled(false);
        alertService.confirm();
    }
}

@@ -81,29 +81,97 @@
 oid.
-content.ComponentName
+app.KeyguardManager;%0Aimport android.content.ComponentName;%0Aimport android.content.Context
 ;%0Aim
@@ -313,78 +313,17 @@
 oid.
-text.Html;%0Aimport android.view.View;%0Aimport android.view.WindowManager
+view.View
 ;%0Aim
@@ -2499,274 +2499,225 @@
     
-getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON%0A                %7C WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD%0A                %7C WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED%0A                %7C WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
+setShowWhenLocked(true);%0A        setTurnScreenOn(true);%0A        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);%0A        keyguardManager.requestDismissKeyguard(this, null
 );%0A 

fix deprecation