package com.example.infiniteLoop.parkingo;

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

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class ticket extends Activity {
    TextView slot,tkt,time_tv,date_tv,vehicle,ticket_title,checkout_label,checkout_time;
    TextView checkout_date_label,checkout_date;
    ArrayList ticketData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ticket_layout);

        slot=findViewById(R.id.slot);
        vehicle=findViewById(R.id.vehicle);
        tkt=findViewById(R.id.ticket);
        time_tv=findViewById(R.id.time_tv);
        date_tv=findViewById(R.id.date_tv);
        ticket_title=findViewById(R.id.ticket_title);
        checkout_label=findViewById(R.id.checkout_label);
        checkout_time=findViewById(R.id.checkout_time);
        checkout_date=findViewById(R.id.checkout_date);
        checkout_date_label=findViewById(R.id.checkout_date_label);
        Intent i=getIntent();
        String ticket_no;
        if(i.hasExtra("ticket_data")){
           ticketData = (ArrayList<String>) i.getSerializableExtra("ticket_data");
           ticket_no = i.getExtras().getString("ticket_no");
        }
        else{
        }

        if(i.hasExtra("checkin")) {
            ticket_no=i.getExtras().getString("ticket_no");
            sqlite_ops so = new sqlite_ops(this);
            so.getTicketDetails(ticket_no,this);
            //waiting popup
        }
        if(i.hasExtra("checkout")){
            ticket_no=i.getExtras().getString("ticket_no");
           // sqlite_ops so=new sqlite_ops(this);
           // ticketData=so.getTicketDetails(ticket_no);
            Log.d("CHECKOUT","successfully checkout out");
            ticket_title.setText("Checkout Receipt");
            checkout_label.setVisibility(View.VISIBLE);
            checkout_time.setVisibility(View.VISIBLE);
            checkout_date.setVisibility(View.VISIBLE);
            checkout_date_label.setVisibility(View.VISIBLE);
            String checkout_date_data=ticketData.get(6).toString();
            String[] checkoutdateandtime=checkout_date_data.split(" ");
            checkout_date.setText(checkoutdateandtime[0]);
            checkout_time.setText(checkoutdateandtime[1]);
        }else {
            ticket_title.setText("Parking Ticket");
            checkout_label.setVisibility(View.INVISIBLE);
            checkout_time.setVisibility(View.INVISIBLE);
            checkout_date.setVisibility(View.INVISIBLE);
            checkout_date_label.setVisibility(View.INVISIBLE);
        }
        if(i.hasExtra("ticket_data")) {

            slot.setText(ticketData.get(1).toString());
            tkt.setText(ticketData.get(0).toString().substring(0, 4).toUpperCase());
            String date_data = ticketData.get(3).toString();
            String[] dateandtime = date_data.split(" ");
            date_tv.setText(dateandtime[0]);
            time_tv.setText(dateandtime[1]);
            vehicle.setText(ticketData.get(2).toString());
        }
    }

    public void showTicketDetails(ArrayList ticketData) {
        slot.setText(ticketData.get(1).toString());
        tkt.setText(ticketData.get(0).toString().substring(0,4).toUpperCase());
        long datetime = Long.parseLong(ticketData.get(3).toString());
        Date date = new Date(datetime*1000);
        Format format = new SimpleDateFormat("dd/MM/yy HH:mm");
        String dateString = format.format(date);
        //String date_data=ticketData.get(3).toString();
        String[] dateandtime=dateString.split(" ");
        date_tv.setText(dateandtime[0]);
        time_tv.setText(dateandtime[1]);
        vehicle.setText(ticketData.get(2).toString());
    }

    public void Print(View view) {
        Toast.makeText(this,"Printing Ticket",Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this,dashboard.class));
        finish();
    }
}
