package au.gov.dto.dibp.appointments.wallet;

import au.gov.dto.dibp.appointments.client.Client;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

@Controller
class WalletBarcodeController {
    private static final String IMAGE_PNG = "image/png";
    private static final int SIZE_PIXELS = 250;

    private final String teamIdentifier;

    @Autowired
    public WalletBarcodeController(@Value("${wallet.team.identifier}") String teamIdentifier) {
        this.teamIdentifier = teamIdentifier;
    }

    @RequestMapping(value = "/wallet/pass/barcode.png", method = RequestMethod.GET, produces = IMAGE_PNG)
    public void passBarcode(@AuthenticationPrincipal Client client,
                            HttpServletRequest request,
                            HttpServletResponse response) throws WriterException, IOException, URISyntaxException {
        response.setContentType("image/png");
        String passUrlPath = String.format("/wallet/v1/passes/%s/citizenship?id=%s&otherid=%s", teamIdentifier, client.getClientId(), client.getCustomerId());
        String passUrl = new URI(request.getRequestURL().toString()).resolve(passUrlPath).toASCIIString();
        BitMatrix bitMatrix = new MultiFormatWriter().encode(passUrl, BarcodeFormat.QR_CODE, SIZE_PIXELS, SIZE_PIXELS);
        BufferedImage barcodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
        ImageIO.write(barcodeImage, "PNG", response.getOutputStream());
    }
}

@@ -1100,36 +1100,40 @@
 te final String 
-team
+passType
 Identifier;%0A%0A   
@@ -1196,20 +1196,25 @@
 %7Bwallet.
-team
+pass.type
 .identif
@@ -1227,20 +1227,24 @@
  String 
-team
+passType
 Identifi
@@ -1262,20 +1262,24 @@
    this.
-team
+passType
 Identifi
@@ -1283,20 +1283,24 @@
 ifier = 
-team
+passType
 Identifi
@@ -1802,12 +1802,16 @@
 s%22, 
-team
+passType
 Iden

fix apple wallet qrcode url