package ca.ualberta.cs.linkai.beep;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;

/**
 * @see RiderMainActivity
 */

public class ViewEstimateActivity extends Activity {

    private EditText currentEstimate;
    private EditText userKeyword;
    private Button placeRequestButton;

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

    @Override
    protected void onStart() {
        super.onStart();

        currentEstimate = (EditText) findViewById(R.id.currentEstimate);
        userKeyword = (EditText) findViewById(R.id.userKeyword);
        placeRequestButton = (Button) findViewById(R.id.placeRequestButton);

        //TODO: change text to current estimate!
        DecimalFormat numberFormat = new DecimalFormat("#.00");
        currentEstimate.setHint("(CAD)" + numberFormat.format(RiderMainActivity.myRequest.getEstimate()));

        //reach here when user click the placeRequest button
        placeRequestButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                setResult(RESULT_OK);

                String keyword;
                Double fare;

                if (currentEstimate.getText().toString().isEmpty()){
                    Toast.makeText(ViewEstimateActivity.this, "Please enter fare you willing to offer", Toast.LENGTH_SHORT).show();
                } else {
                    try {
                        fare = Double.parseDouble(currentEstimate.getText().toString());
                        RiderMainActivity.myRequest.setFare(fare);

                        keyword = userKeyword.getText().toString();
                        if(keyword.isEmpty()) {
                            keyword = "The rider doesn't left any info";
                        }

                        RiderMainActivity.myRequest.setKeyword(keyword);

                        RuntimeRequestList.getInstance().myRequestList.add(RiderMainActivity.myRequest);

                        ElasticsearchRequestController.AddRequestListTask addRequestListTask = new ElasticsearchRequestController.AddRequestListTask();
                        addRequestListTask.execute(RuntimeRequestList.getInstance().myRequestList);

                        // add request to request list
                        //RiderMainActivity.currentAccount.requestsList.add(RiderMainActivity.myRequest);

                        // change the number of requests the current user has
                        //RiderMainActivity.currentAccount.setRequestNum(RiderMainActivity.currentAccount.getRequestNum() + 1);
                        // update to the elastic search server
                        //ElasticsearchAccountController.AddAccountTask addAccountTask = new ElasticsearchAccountController.AddAccountTask();
                        //addAccountTask.execute(RiderMainActivity.currentAccount);
                        Toast.makeText(ViewEstimateActivity.this, "Request has been sent, please wait for drivers to accept.", Toast.LENGTH_SHORT).show();
                        //destroy this page, return to last page
                        finish();


                    } catch (Exception e) {
                        Log.e("Error", "Something was wrong when we placed the request!");
                    }
                }
            }
        });
    }
}

@@ -271,31 +271,291 @@
 ort 
-java.text.DecimalFormat
+com.google.android.gms.tasks.RuntimeExecutionException;%0Aimport com.google.gson.Gson;%0A%0Aimport java.io.FileNotFoundException;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.OutputStreamWriter;%0Aimport java.text.DecimalFormat;%0A%0Aimport java.io.FileInputStream
 ;%0A%0A/
@@ -754,16 +754,72 @@
 utton;%0A%0A
+    private static final String FILENAME = %22file.sav%22;%0A%0A
     @Ove
@@ -1030,24 +1030,24 @@
 onStart() %7B%0A
-
         supe
@@ -1056,24 +1056,47 @@
 onStart();%0A%0A
+        saveInFile();%0A%0A
         curr
@@ -3950,33 +3950,1099 @@
   %7D%0A
-            %7D%0A        %7D);
+%0A%0A                clearInFile();%0A                saveInFile();%0A%0A%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void saveInFile()%7B%0A        try %7B%0A            FileOutputStream fos = openFileOutput(FILENAME, 0);%0A            OutputStreamWriter writer = new OutputStreamWriter(fos);%0A            Gson gson = new Gson();%0A            gson.toJson(RuntimeRequestList.getInstance().myRequestList, writer);%0A            writer.flush();%0A        %7D catch (FileNotFoundException e)%7B%0A            throw new RuntimeException();%0A        %7D catch (IOException e)%7B%0A            throw new RuntimeException();%0A        %7D%0A    %7D%0A%0A    private void clearInFile()%7B%0A        try %7B%0A            FileOutputStream fos = openFileOutput(FILENAME, 0);%0A            OutputStreamWriter writer = new OutputStreamWriter(fos);%0A            Gson gson = new Gson();%0A            gson.toJson(RuntimeRequestList.getInstance().myRequestList, writer);%0A            writer.flush();%0A        %7D catch (FileNotFoundException e)%7B%0A            throw new RuntimeException();%0A        %7D catch (IOException e)%7B%0A            throw new RuntimeException();%0A        %7D
 %0A   

save the request on the file for offline use