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.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

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

/*! \class A ticket class
    A class to generate ticket 
*/
public class ticket extends Activity {
    LinearLayout checkout_parent,checkout_date_parent;
    LinearLayout charge_parent;
    TextView slot,tkt,time_tv,date_tv,vehicle,ticket_title,checkout_time,charge_value;
    TextView checkout_date;
    ArrayList ticketData;
    ProgressDialog progress;
    @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_parent = findViewById(R.id.checkout_parent);
        charge_parent = findViewById(R.id.charge_parent);
        checkout_date_parent = findViewById(R.id.checkout_date_parent);
        charge_value = findViewById(R.id.charge_value);
        checkout_time=findViewById(R.id.checkout_time);
        checkout_date=findViewById(R.id.checkout_date);
        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);
            progress = new ProgressDialog(ticket.this);
            progress.setTitle("Checking in");
            progress.setMessage("Getting ticket details...");
            progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
            progress.show();
            //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_parent.setVisibility(View.VISIBLE);
            //checkout_time.setVisibility(View.VISIBLE);
            checkout_date_parent.setVisibility(View.VISIBLE);
            charge_parent.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]);
            charge_value.setText("\u20B9 "+ticketData.get(7).toString());
        }else {
            ticket_title.setText("Parking Ticket");
            checkout_parent.setVisibility(View.GONE);
            //checkout_time.setVisibility(View.INVISIBLE);
            //checkout_date.setVisibility(View.INVISIBLE);
            checkout_date_parent.setVisibility(View.GONE);
            charge_parent.setVisibility(View.GONE);
        }
        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) {
        progress.dismiss();
        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();
    }
}