/*
 Copyright by Boston University, 2016
 Authors: Josejulio Martínez, Daniel Kornhauser
 Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 */
package com.aluxoft.earrecognition.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.aluxoft.earrecognition.R;
import com.aluxoft.earrecognition.common.TopMatchesList;
import com.aluxoft.earrecognition.common.NameDistance;
import com.google.gson.Gson;

import java.util.UUID;

/**
 * Shows top 10 matches, allows to register if not match is satisfactory.
 * TODO: Populate list
 */
public class SelectFollowupActivity extends Activity {

    public TopMatchesList topMatchesList = null;

    private static final int COMMCARE_REGISTER = 1;
    private static final int COMMCARE_FOLLOW_UP = 2;

    //private TopMatchesList topMatchesList;

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

        String list = this.getIntent().getStringExtra("list");
        this.topMatchesList = new Gson().fromJson(list, TopMatchesList.class);
        Button registerButton = (Button) findViewById(R.id.register_commcare);

        ArrayAdapter<NameDistance> arrayAdapter = new ArrayAdapter<NameDistance>(
                this,
                android.R.layout.simple_list_item_1,
                this.topMatchesList.getTop()
        );

        ListView topNListView = (ListView)findViewById(R.id.topNListView);
        topNListView.setAdapter(arrayAdapter);
        topNListView.setItemsCanFocus(true);

        topNListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("case_id", topMatchesList.getTop().get(position).patient.getCaseId());
                SelectFollowupActivity.this.setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("case_id", UUID.randomUUID().toString());
                SelectFollowupActivity.this.setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case COMMCARE_REGISTER:
            case COMMCARE_FOLLOW_UP:
            {
                Intent intent = new Intent(this, EarCaptureActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
                break;
        }
    }
}
