package com.example.infiniteLoop.parkingo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


import static android.content.ContentValues.TAG;

/*! \class A checkin class
    
*/

public class checkin extends Activity {
    EditText name,mobile,vehicle,slot;
    ProgressDialog progress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkin_layout);
        name=findViewById(R.id.name);
        slot=findViewById(R.id.slot);
        mobile=findViewById(R.id.mobile);
        vehicle=findViewById(R.id.vehicle);

    }
    /** @brief Takes the user to the activity where he can chose the slot
    @param view - View of the calling activity
    @return void
    */
    public void getslot( View v) {
        Log.d(TAG, "getSlot: going to take result");
        Intent getslot = new Intent(checkin.this, category_wise_availability.class);
        getslot.putExtra("callingActivity","checkin");
        startActivityForResult(getslot, 1);
    }
    /** @brief Sets the slot edittext
    @param requestCode - an integer argument
    @param resultCode - an integer argument
    @param data - An Intent message object
    @return void
    */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onActivityResult: "+resultCode);
        //super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1){
            if(resultCode==RESULT_OK){
                String res=data.getStringExtra("result");
                slot.setText(res);
            }
            else{
                slot.setError("no slot free");
            }
        }
    }
    /** @brief Gets the check-in details from the view
    @param view - view of the calling activity
    @return void
    */
    public  void check_in_data(View view){
        checkin_details cd=new checkin_details();
        cd.setName(name.getText().toString().trim());
        cd.setMobile(Long.parseLong( mobile.getText().toString().trim()));
        cd.setVehicle_no(vehicle.getText().toString().trim());
        cd.setSlot(slot.getText().toString().trim());

        if(!cd.getName().isEmpty() && !cd.getSlot().isEmpty() && !cd.getVehicle_no().isEmpty()){
            sqlite_ops so=new sqlite_ops(this);
            so.check_in(cd,this);
            progress = new ProgressDialog(checkin.this);
            progress.setTitle("Checking in");
            progress.setMessage("Booking a slot...");
            progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
            progress.show();
            //add waiting popup
        }
        else{
            Toast.makeText(this,"Fields can not be empty",Toast.LENGTH_SHORT).show();
        }
    }
    public void checkinSuccess(String ticketNo) {
        progress.dismiss();
        Intent i=new Intent(checkin.this,ticket.class);
        i.putExtra("ticket_no",ticketNo);
        i.putExtra("checkin",1);
        startActivity(i);
    }

    public void checkinFailure() {
        progress.dismiss();
        Toast.makeText(this,"Error in inserting data",Toast.LENGTH_SHORT).show();

    }

    /** @brief Takes the user to the previous activity if back button is pressed
    @return void
    */
    @Override
    public void onBackPressed() {
        startActivity(new Intent(this,dashboard.class));
        finish();
    }
}
