<?php

namespace App\Helpers;

class InseoHelper
{
    public static function tglbulanindo2($waktu, $tipe = '')
    {
        $menit = substr($waktu, 14, 2);
        $jam = substr($waktu, 11, 2);
        $tgl = substr($waktu, 8, 2);
        $bln = substr($waktu, 5, 2);
        $thn = substr($waktu, 0, 4);
        $bulan = array('Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'Nopember', 'Desember');
        $idxhari = date('N', strtotime($waktu));

        switch ($tipe) {
            case 1:
                $full = $tgl.' '.$bulan[(int) $bln - 1].' '.$thn;
                break;
            case 2:
                $full = $tgl.'/'.$bln.'/'.$thn;
                break;
            default:
                $full = "$tgl ".$bulan[(int) $bln - 1]." $thn";
        }

        return $full;
    }

    public static function tgl($waktu)
    {
        $tgl = substr($waktu, 8, 2);
        $bln = substr($waktu, 5, 2);
        $thn = substr($waktu, 0, 4);

        $full = $tgl.' - '.$bln.' - '.$thn;

        return $full;
    }

    public static function inseoencrypt($str, $kunci = 'asd')
    {
        $hasil = '';

        for ($i = 0; $i < strlen($str); ++$i) {
            $karakter = substr($str, $i, 1);
            $kuncikarakter = substr($kunci, ($i % strlen($kunci)) - 1, 1);
            $karakter = chr(ord($karakter) + ord($kuncikarakter));
            $hasil .= $karakter;
        }

        return urlencode(base64_encode($hasil));
    }

    public static function inseodecrypt($str, $kunci = 'asd')
    {
        $str = base64_decode(urldecode($str));
        $hasil = '';

        for ($i = 0; $i < strlen($str); ++$i) {
            $karakter = substr($str, $i, 1);
            $kuncikarakter = substr($kunci, ($i % strlen($kunci)) - 1, 1);
            $karakter = chr(ord($karakter) - ord($kuncikarakter));
            $hasil .= $karakter;
        }

        return $hasil;
    }
}
