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("org.commcare.dalvik.action.CommCareSession");
                String sssd = "";
                sssd +=
                        "COMMAND_ID" + " " + "m1" + " "
                                +
                                "CASE_ID" + " " + "case_id" + " " + earIdList.getTop().get(position).person.getCaseId() + " " /* + // "99de08b8-7235-41ba-9dbe-d1e5d70575cf" + " " +
                                "COMMAND_ID" + " " + "m1-f0" */;
                intent.putExtra("ccodk_session_request", sssd);
                SelectFollowupActivity.this.startActivityForResult(intent, COMMCARE_FOLLOW_UP);
            }
        });

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent("org.commcare.dalvik.action.CommCareSession");
                String sssd = "COMMAND_ID" + " " + "root" + " " + "COMMAND_ID" + " " + "m0-f0";
                i.putExtra("ccodk_session_request", sssd);
                SelectFollowupActivity.this.startActivityForResult(i, COMMCARE_REGISTER);
            }
        });
    }

    @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;
        }
    }
}
