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.EarIdList;
import com.aluxoft.earrecognition.common.NameDistance;
import com.google.gson.Gson;

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

    public EarIdList earIdList = null;

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

    //private EarIdList earIdList;

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

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

        ArrayAdapter<NameDistance> arrayAdapter = new ArrayAdapter<NameDistance>(
                this,
                android.R.layout.simple_list_item_1,
                this.earIdList.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", earIdList.getTop().get(position).person.getCaseId());
                SelectFollowupActivity.this.setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectFollowupActivity.this.setResult(Activity.RESULT_OK, new 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;
        }
    }
}
