import { Controller, Post, Body } from '@nestjs/common'
import * as zlib from 'zlib'

@Controller('print')
export class PrintController {

  @Post()
  async printReceipt(@Body() body: any) {

    const now = new Date().toLocaleString("sv-SE").replace("T", " ");

    const formatRupiah = (number: number) =>
      'Rp' + Number(number).toLocaleString('id-ID')

    const page1 = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>

@page{
margin:0;
}

body{
margin:0;
font-family: monospace;
}

/* 
Calculate row height
line-height ≈ 1.2 . font-size
16px * 1.2 = 19.2px
Add some padding for better readability
2px top + 2px bottom = 4px
Total row height = 19.2px + 4px = 23.2px
Add or Subtract total row height to the padding
 */

.receipt{
width:480px; /* 60mm @203dpi */
padding:12px 12px 87px 50px;
position:relative;
box-sizing:border-box;
}

h2{
font-size:20px;
margin:0 0 8px 0;
text-align:center;
}

table{
border-collapse:collapse;
font-size:18px;
}

td{
padding:2px 0;
white-space:nowrap;
}

.label{
padding-right:6px;
}

.separator{
padding-right:6px;
}

.value{
text-align:right;
padding-left:10px;
}

.total-row td{
font-weight:bold;
padding-top:6px;
}

.vertical-date{
position:absolute;
left:4px;
top:45px;
writing-mode: vertical-rl;
font-size:18px;
border-right:2px dotted #000;
padding-right:4px;
}

</style>
</head>
<body>

<div class="receipt">

<div class="vertical-date">
${now}
</div>

<h2>${body.storeName}</h2>

<table>

<tr>
<td class="label">Kode Item</td>
<td class="separator">:</td>
<td class="value">${body.kodeBarang}</td>
</tr>

<tr>
<td class="label">Tipe</td>
<td class="separator">:</td>
<td class="value">${body.type}</td>
</tr>

<tr>
<td class="label">Berat Kotor</td>
<td class="separator">:</td>
<td class="value">${body.beratKotor}&nbsp;gr</td>
</tr>

<tr>
<td class="label">Berat Bersih</td>
<td class="separator">:</td>
<td class="value">${body.beratBersih}&nbsp;gr</td>
</tr>

<tr>
<td class="label">Karat</td>
<td class="separator">:</td>
<td class="value">${body.kadarKarat}</td>
</tr>

<tr>
<td class="label">Kadar (%)</td>
<td class="separator">:</td>
<td class="value">${body.kadarPersen}&nbsp;%</td>
</tr>

<tr>
<td class="label">Harga/gr</td>
<td class="separator">:</td>
<td class="value">${formatRupiah(body.hargaPerGram)}</td>
</tr>

<tr class="total-row">
<td class="label">TOTAL</td>
<td class="separator">:</td>
<td class="value">${formatRupiah(body.total)}</td>
</tr>
</table>
</div>
</body>
</html>
`

    const page2 = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>

@page{
margin:0;
}

body{
margin:0;
font-family: monospace;
}

/* 
Calculate row height
line-height ≈ 1.2 . font-size
16px * 1.2 = 19.2px
Add some padding for better readability
2px top + 2px bottom = 4px
Total row height = 19.2px + 4px = 23.2px
Add or Subtract total row height to the padding
 */

.receipt{
width:480px; /* 60mm @203dpi */
padding:12px 12px 87px 50px;
position:relative;
box-sizing:border-box;
}

h2{
font-size:20px;
margin:0 0 8px 0;
text-align:center;
}

table{
border-collapse:collapse;
font-size:18px;
}

td{
padding:2px 0;
white-space:nowrap;
}

.label{
padding-right:6px;
}

.separator{
padding-right:6px;
}

.value{
text-align:right;
padding-left:10px;
}

.total-row td{
font-weight:bold;
padding-top:6px;
}

.vertical-date{
position:absolute;
left:4px;
top:45px;
writing-mode: vertical-rl;
font-size:18px;
border-right:2px dotted #000;
padding-right:4px;
}

</style>
</head>
<body>

<div class="receipt">

<div class="vertical-date">
${now}
</div>

<h2>${body.storeName}</h2>

<table>

<tr>
<td class="label">Kode Item</td>
<td class="separator">:</td>
<td class="value">${body.kodeBarang}</td>
</tr>

<tr>
<td class="label">Tipe</td>
<td class="separator">:</td>
<td class="value">${body.type}</td>
</tr>

<tr>
<td class="label">Berat Kotor</td>
<td class="separator">:</td>
<td class="value">${body.beratKotor}&nbsp;gr</td>
</tr>

<tr>
<td class="label">Berat Bersih</td>
<td class="separator">:</td>
<td class="value">${body.beratBersih}&nbsp;gr</td>
</tr>

<tr>
<td class="label">Karat</td>
<td class="separator">:</td>
<td class="value">${body.kadarKarat}</td>
</tr>

<tr>
<td class="label">Kadar (%)</td>
<td class="separator">:</td>
<td class="value">${body.kadarPersen}&nbsp;%</td>
</tr>

<tr>
<td class="label">&nbsp;/gr</td>
<td class="separator">&nbsp;</td>
<td class="value">&nbsp;&nbsp;</td>
</tr>

<tr class="total-row">
<td class="label">&nbsp;</td>
<td class="separator">&nbsp;</td>
<td class="value">&nbsp;&nbsp;</td>
</tr>
</table>
</div>
</body>
</html>
`

    const pages = [page1, page2]
    const json = JSON.stringify(pages)

    const gzipped = zlib.gzipSync(json)
    const base64 = gzipped.toString('base64')

    const intentUrl =
      'intent://#Intent;' +
      'scheme=print-intent;' +
      'S.content=' + encodeURIComponent(base64) + ';' +
      'end;'

    return { intentUrl }
  }
}