package io.trustbuilder.mfa.android.demoapp.screen;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;

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

import io.trustbuilder.mfa.android.demoapp.R;
import io.trustbuilder.mfa.lib.usecase.definition.authsessioncancellation.AuthSessionCancellation;
import io.trustbuilder.mfa.lib.usecase.definition.authsessioncancellation.AuthSessionCancellationParams;
import io.trustbuilder.mfa.lib.usecase.definition.authsessioncancellation.CancelReason;

public class AuthSessionCancellationActivity extends TrustBuilderActivity {
    private EditText sessionId;
    private String cancelReason = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: AuthSessionCancellationUseCase");
        setTitle(this.getClass().getSimpleName());
        setContentView(R.layout.activity_cancel_auth);
        dropdownCancelReasonSpinner(R.id.cancelReason_CancelAuth);
        this.proceed = this.findViewById(R.id.proceed_CancelAuth);
        this.tknAlias = this.findViewById(R.id.tknAlias_CancelAuth);
        this.sessionId = this.findViewById(R.id.sessionId_CancelAuth);
        this.result = this.findViewById(R.id.result_CancelAuth);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: AuthSessionCancellationUseCase");
        this.proceed.setOnClickListener((v) -> {
            if (checkMandatoryField(tknAlias, "tknAlias")) return;
            if (checkMandatoryField(sessionId, "sessionId")) return;
            try {
                AuthSessionCancellationParams request = new AuthSessionCancellationParams(tknAlias.getText().toString(),
                        sessionId.getText().toString(), CancelReason.fromValue(cancelReason));
                AuthSessionCancellation authSessionCancellation = mfaLib.startAuthSessionCancellation(request);
                displayJsonResult(authSessionCancellation.execute());
            } catch (Exception e) {
                displayException(e);
            }
        });
    }

    protected void dropdownCancelReasonSpinner(int id) {
        String[] items = new String[]{"NONE", "DUPLICATE", "FRAUDULENT", "ABANDONED"};
        Spinner dropdown = findViewById(id);
        List<String> itemsList = new ArrayList<>
                (Arrays.asList(items));
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, itemsList) {

            @Override
            public View getDropDownView(
                    int position, View convertView,
                    @NonNull ViewGroup parent) {
                View view = super.getDropDownView(
                        position, convertView, parent);
                TextView textView = (TextView) view;
                if (position == 0) {
                    textView.setTextColor(Color.GRAY);
                } else {
                    textView.setTextColor(Color.BLACK);
                }
                return view;
            }
        };
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        dropdown.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(
                            AdapterView<?> parent, View view,
                            int position, long id) {
                        if (cancelReason == "") {
                            cancelReason = "NONE";
                        } else {
                            cancelReason = (String) parent
                                    .getItemAtPosition(position);
                        }
                    }

                    @Override
                    public void onNothingSelected(
                            AdapterView<?> parent) {
                    }
                });
        dropdown.setAdapter(adapter);
    }
}