package com.teamltt.carcare.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.teamltt.carcare.R;
import com.teamltt.carcare.database.DbHelper;
import com.teamltt.carcare.model.Vehicle;

public class CarInfoEditActivity extends AppCompatActivity {

    private static final String TAG = "CarInfoEditActivity";

    private boolean edited = false;
    private DbHelper dbHelper;
    long vehicleId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_info_edit);

        vehicleId = getIntent().getLongExtra(CarInfoActivity.EXTRA_VEHICLE_ID, -1);
        dbHelper = new DbHelper(CarInfoEditActivity.this);
        if (vehicleId == -1) {
            Log.e(TAG, "invalid vehicle id");
            finish();
        }
        Vehicle vehicle = dbHelper.getVehicle(vehicleId);
        if (vehicle == null || vehicleId == -1) {
            Log.e(TAG, "could not get vehicle properly");
        } else {
            ((TextView) findViewById(R.id.fieldVIN)).setText(vehicle.getVin());
            ((TextView) findViewById(R.id.fieldMake)).setText(vehicle.getMake());
            ((TextView) findViewById(R.id.fieldModel)).setText(vehicle.getModel());
            ((TextView) findViewById(R.id.fieldColor)).setText(vehicle.getColor());
            ((TextView) findViewById(R.id.fieldNickname)).setText(vehicle.getNickname());
            ((TextView) findViewById(R.id.fieldPlate)).setText(vehicle.getPlateNumber());
        }
    }


    public void saveInfo(View view) {
        Log.i(TAG, "saveInfo");
        edited = true;
        back(view);
    }

    public void back(View view) {
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
        if (edited) {
            String vin = ((TextView) findViewById(R.id.fieldVIN)).getText().toString();
            String make = ((TextView) findViewById(R.id.fieldMake)).getText().toString();
            String model = ((TextView) findViewById(R.id.fieldModel)).getText().toString();
            String year = ((TextView) findViewById(R.id.fieldYear)).getText().toString();
            String color = ((TextView) findViewById(R.id.fieldColor)).getText().toString();
            String nickname = ((TextView) findViewById(R.id.fieldNickname)).getText().toString();
            String plateNumber = ((TextView) findViewById(R.id.fieldPlate)).getText().toString();

            Vehicle vehicle = new Vehicle(vin, make, model, year, color, nickname, plateNumber);

            int numAffected = dbHelper.updateVehicle(vehicleId, vehicle);

            if (numAffected == 0) {
                Log.e(TAG, "database not updated");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dbHelper.close();
    }
<<<<<<< HEAD

    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
            case (R.id.action_carInfo):
                intent = new Intent(this, CarInfoActivity.class);
                startActivity(intent);
                break;
            case (R.id.action_demo):
                intent = new Intent(this, DemoActivity.class);
                startActivity(intent);
                break;
            case (R.id.action_trips):
                intent = new Intent(this, TripsActivity.class);
                startActivity(intent);
            case (R.id.action_dynamic):
                intent = new Intent(this, DynamicActivity.class);
                startActivity(intent);
                break;
            case (R.id.action_reminder):
                intent = new Intent(this, ReminderActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void toggleLogging(MenuItem item) {
        //
    }
=======
>>>>>>> origin/simulator
}

@@ -3131,1110 +3131,8 @@
   %7D%0A
-%3C%3C%3C%3C%3C%3C%3C HEAD%0A%0A    public boolean onOptionsItemSelected(MenuItem item) %7B%0A        Intent intent;%0A        switch (item.getItemId()) %7B%0A            case (R.id.action_carInfo):%0A                intent = new Intent(this, CarInfoActivity.class);%0A                startActivity(intent);%0A                break;%0A            case (R.id.action_demo):%0A                intent = new Intent(this, DemoActivity.class);%0A                startActivity(intent);%0A                break;%0A            case (R.id.action_trips):%0A                intent = new Intent(this, TripsActivity.class);%0A                startActivity(intent);%0A            case (R.id.action_dynamic):%0A                intent = new Intent(this, DynamicActivity.class);%0A                startActivity(intent);%0A                break;%0A            case (R.id.action_reminder):%0A                intent = new Intent(this, ReminderActivity.class);%0A                startActivity(intent);%0A                break;%0A        %7D%0A        return super.onOptionsItemSelected(item);%0A    %7D%0A%0A    public void toggleLogging(MenuItem item) %7B%0A        //%0A    %7D%0A=======%0A%3E%3E%3E%3E%3E%3E%3E origin/simulator
+%0A
 %0A%7D%0A

Remove menu from car edit info