<?php
/**
 * @package web-livedraw
 * @since 1.0.0
 */

class Exito_Post
{
    public $next_day = [
        // prediksi
        'prediksi-togel-sydney-night',
        'prediksi-togel-cambodia-night',
        'prediksi-togel-china-night',
        'prediksi-togel-macau-night',
        'prediksi-togel-magnum-4d',
        'prediksi-togel-brazil-night-pools',
        'prediksi-togel-watford-pools',
        'prediksi-togel-jakarta-pools',
        'prediksi-togel-jowo-pools',
        'prediksi-togel-canadia-pools',
        'prediksi-togel-kentucky-midday',
        'prediksi-togel-florida-midday',
        'prediksi-togel-carolina-day',
        'prediksi-togel-vietnam-lottery',
        'prediksi-togel-oregon-03',
		'prediksi-togel-kupang-lotto',
		'prediksi-togel-vienna-pools',
		'prediksi-togel-ascoli-pools',

        // syair
        'syair-togel-sydney-night',
        'syair-togel-cambodia-night',
        'syair-togel-china-night',
        'syair-togel-macau-night',
        'syair-togel-magnum-4d',
        'syair-togel-brazil-night-pools',
        'syair-togel-watford-pools',
        'syair-togel-jakarta-pools',
        'syair-togel-jowo-pools',
        'syair-togel-canadia-pools',
    ];

    function __construct()
    {
        //
    }

    function get_older_post( $category_name, $status = 'publish', $limit = 1 )
    {
        $posts = get_posts([ 'numberposts' => $limit, 'category_name' => $category_name, 'post_status' => $status ]);
        return ( $posts == null ) ? null : ( ( $limit == 1 ) ? $posts[0] : $posts );
    }

    function generate_post( $post_date, $main_category, $post_category, $is_prediksi = true )
    {
        $post_tgl = ( in_array( $post_category, $this->next_day ) ) ? strtotime( $post_date . '+2days' ) : strtotime( $post_date . '+1days' );
        $post_old = $this->get_older_post( $post_category, 'contoh' );
        $post_txt = ucwords( str_replace( '-', ' ', $post_category ) ). ' - ' . date_i18n( 'd F Y', $post_tgl );
        $post_tag = wp_get_post_tags( $post_old->ID, [ 'fields' => 'names' ] );
        $post_new = wp_insert_post([
            'post_type'     => 'post',
            'post_title'    => $post_txt,
            'post_status'   => 'publish',
            'post_date'     => date_i18n( 'Y-m-d', strtotime( $post_date . '+1days' ) ) . ' ' . date( 'H:i:s', strtotime( $post_old->post_date ) ),
            'post_name'     => sanitize_title( $post_txt ),
            'post_content'  => str_replace( '[prediksi]', $this->generate_number( $post_txt, $post_category ), $post_old->post_content ),
            'post_author'   => 1,
            'post_category' => [
                get_category_by_slug( $main_category )->term_id,
                get_category_by_slug( $post_category )->term_id
            ],
        ]);

        set_post_thumbnail( $post_new, get_post_thumbnail_id( $post_old->ID ) );

        wp_set_post_tags( $post_new, $post_tag );
    }

    function generate_number( $post_txt, $post_category )
    {
        $numb = 1;
        $html = '';

        if ( $post_category == 'prediksi-togel-toto-macau' ) {
            $numb += 4;
        } elseif ( $post_category == 'prediksi-togel-toto-macau-5d' ) {
            $numb += 1;
        } elseif ( $post_category == 'prediksi-togel-brunei' ) {
            $numb += 2;
        } elseif ( $post_category == 'prediksi-togel-carolina' ) {
            $numb += 1;
        } elseif ( $post_category == 'prediksi-togel-chelsea' ) {
            $numb += 3;
        } elseif ( $post_category == 'prediksi-togel-florida' ) {
            $numb += 1;
        } elseif ( $post_category == 'prediksi-togel-kentucky' ) {
            $numb += 1;
        } elseif ( $post_category == 'prediksi-togel-newyork' ) {
            $numb += 1;
        } elseif ( $post_category == 'prediksi-togel-oregon' ) {
            $numb += 3;
        } elseif ( $post_category == 'prediksi-togel-poipet' ) {
            $numb += 3;
        } elseif ( $post_category == 'prediksi-togel-jowo-pools' ) {
            $numb += 1;
        }

        for ( $n = 1; $n <= $numb; $n++ ) {
            $html .= '<div class="content-prediksi text-center px-3 py-3 mb-3">';

            if ( $numb > 1 ) {
                if ( $post_category == 'prediksi-togel-brunei' ) {
                    $text_time = '';

                    switch ( $n ) {
                        case 1 : $text_time = '02 - '; break;
                        case 2 : $text_time = '14 - '; break;
                        case 3 : $text_time = '21 - '; break;
                    }

                    $html .= '<h4 class="prediksi-title text-white mb-0">' . str_replace( '-', $text_time, $post_txt ) . '</h4>';
                } elseif ( $post_category == 'prediksi-togel-carolina' ) {
                    $text_time = '';
                    
                    switch ( $n ) {
                        case 1 : $text_time = 'Day - '; break;
                        case 2 : $text_time = 'Evening - '; break;
                    }

                    $html .= '<h4 class="prediksi-title text-white mb-0">' . str_replace( '-', $text_time, $post_txt ) . '</h4>';
                } elseif ( $post_category == 'prediksi-togel-chelsea' ) {
                    $text_time = '';
                    
                    switch ( $n ) {
                        case 1 : $text_time = '11 - '; break;
                        case 2 : $text_time = '15 - '; break;
                        case 3 : $text_time = '19 - '; break;
                        case 4 : $text_time = '21 - '; break;
                    }

                    $html .= '<h4 class="prediksi-title text-white mb-0">' . str_replace( '-', $text_time, $post_txt ) . '</h4>';
                } elseif ( $post_category == 'prediksi-togel-florida' ) {
                    $text_time = '';
                    
                    switch ( $n ) {
                        case 1 : $text_time = 'Midday - '; break;
                        case 2 : $text_time = 'Evening - '; break;
                    }

                    $html .= '<h4 class="prediksi-title text-white mb-0">' . str_replace( '-', $text_time, $post_txt ) . '</h4>';
                } elseif ( $post_category == 'prediksi-togel-kentucky' ) {
                    $text_time = '';
                    
                    switch ( $n ) {
                        case 1 : $text_time = 'Midday - '; break;
                        case 2 : $text_time = 'Evening - '; break;
                    }

                    $html .= '<h4 class="prediksi-title text-white mb-0">' . str_replace( '-', $text_time, $post_txt ) . '</h4>';
                } elseif ( $post_category == 'prediksi-togel-newyork' ) {
                    $text_time = '';
                    
                    switch ( $n ) {
                        case 1 : $text_time = 'Midday - '; break;
                        case 2 : $text_time = 'Evening - '; break;
                    }

                    $html .= '<h4 class="prediksi-title text-white mb-0">' . str_replace( '-', $text_time, $post_txt ) . '</h4>';
                } elseif ( $post_category == 'prediksi-togel-oregon' ) {
                    $text_time = '';
                    
                    switch ( $n ) {
                        case 1 : $text_time = '03 - '; break;
                        case 2 : $text_time = '06 - '; break;
                        case 3 : $text_time = '09 - '; break;
                        case 4 : $text_time = '12 - '; break;
                    }

                    $html .= '<h4 class="prediksi-title text-white mb-0">' . str_replace( '-', $text_time, $post_txt ) . '</h4>';
                } elseif ( $post_category == 'prediksi-togel-poipet' ) {
                    $text_time = '';
                    
                    switch ( $n ) {
                        case 1 : $text_time = '12 - '; break;
                        case 2 : $text_time = '15 - '; break;
                        case 3 : $text_time = '19 - '; break;
                        case 4 : $text_time = '22 - '; break;
                    }

                    $html .= '<h4 class="prediksi-title text-white mb-0">' . str_replace( '-', $text_time, $post_txt ) . '</h4>';
                } elseif ( $post_category == 'prediksi-togel-jowo-pools' ) {
                    $text_time = '';
                    
                    switch ( $n ) {
                        case 1 : $text_time = 'Pagi - '; break;
                        case 2 : $text_time = 'Malam - '; break;
                    }

                    $html .= '<h4 class="prediksi-title text-white mb-0">' . str_replace( '-', $text_time, $post_txt ) . '</h4>';
                } else {
                    $html .= '<h4 class="prediksi-title text-white mb-0">' . str_replace( '-', 'SEASON ' . $n . ' - ', $post_txt ) . '</h4>';
                }
            } else {
                $html .= '<h4 class="prediksi-title text-white mb-0">' . $post_txt . '</h4>';
            }

            foreach ( $this->generate_data() as $key => $val ):
                $html .= '<h5 class="text-white mb-1">';
                $html .= '<span class="font-weight-bolder">' . strtoupper(str_replace('_', ' ', $key)) . '</span>';
                $html .= '<span> : </span>';
                $html .= '<span class="text-gold">' . $val . '</span>';
                $html .= '</h5>';
            endforeach;

            $html .= '<h5 class="font-weight-bold mb-0">';
            $html .= '<span class="text-gold">☆☆☆</span>';
            $html .= '<span class="text-white">&ensp;'. get_bloginfo( 'name' ) .'&ensp;</span>';
            $html .= '<span class="text-gold">☆☆☆</span></h5>';
            $html .= '</div>';
        }
    
        return $html;
    }

    function generate_data()
    {
        $bbfs = $this->generate_bbfs();
        $main = $this->generate_play( $bbfs );
        $twin = $this->generate_twin( $main );
        $bb4d = $this->generate_bb4d( $main );
        $bb3d = $this->generate_bb3d( $main );
        $bb2d = $this->generate_bb2d( $main );
        $odd1 = $this->generate_odd1( $main );

        return [
            'angka_main'     => $main,
            'colok_bebas_2D' => $odd1[0].' & '.$odd1[1],
            'top_4D'         => $bb4d[0].'*'.$bb4d[1].'*'.$bb4d[2].'*'.$bb4d[3],
            'top_3D'         => $bb3d[0].'*'.$bb3d[1].'*'.$bb3d[2].'*'.$bb3d[3].'*'.$bb3d[4],
            'top_2D'         => $bb2d[0].'*'.$bb2d[1].'*'.$bb2d[2].'*'.$bb2d[3].'*'.$bb2d[4].'*'.$bb2d[5],
            'shio'           => $this->generate_shio( $bb2d[0] ),
            'angka_twins'    => $twin[0],
        ];
    }
    
    function generate_bbfs()
    {
        $length = 5;
        $number = range(0, 9);
        
        shuffle( $number );

        return implode( '', array_slice( $number, 0, $length ) );
    }
    
    function generate_play( $bbfs )
    {
        $data = array_map( 'intval', str_split( $bbfs ) );
        
        shuffle( $data );

        return $data[0].' '.$data[1].' '.$data[2].' '.$data[3].' '.$data[4];
    }
    
    function generate_twin( $main )
    {
        $data = array_map( 'intval', str_split( str_replace( ' ', '', $main ) ) );
        
        shuffle( $data );

        return [$data[1].$data[1], $data[2].$data[2], $data[3].$data[3]];
    }
    
    function generate_bb4d( $main )
    {
        $data = array_map( 'intval', str_split( str_replace( ' ', '', $main ) ) );
        $list = [
            $data[0].$data[1].$data[2].$data[3],
            $data[4].$data[1].$data[0].$data[2],
            $data[3].$data[4].$data[2].$data[1],
            $data[1].$data[2].$data[4].$data[3],
        ];

        shuffle( $list );

        return $list;
    }
    
    function generate_bb3d( $main )
    {
        $data = array_map( 'intval', str_split( str_replace( ' ', '', $main ) ) );
        $list = [
            $data[0].$data[1].$data[2],
            $data[1].$data[2].$data[3],
            $data[2].$data[3].$data[0],
            $data[3].$data[0].$data[1],
            $data[3].$data[1].$data[0],
        ];

        shuffle( $list );

        return $list;
    }
    
    function generate_bb2d( $main )
    {
        $data = array_map( 'intval', str_split( str_replace( ' ', '', $main ) ) );
        $list = [
            $data[0].$data[1], $data[1].$data[0],
            $data[0].$data[2], $data[1].$data[2],
            $data[0].$data[3], $data[1].$data[3],
            $data[2].$data[1], $data[2].$data[0],
            $data[2].$data[3], $data[3].$data[1],
        ];

        shuffle( $list );

        for ($i = 0; $i <= 5; $i++) {
            if ( $this->generate_shio( $list[0] ) == $this->generate_shio( $list[9] ) ) {
                shuffle( $list );
            } else {
                return $list;
            }
        }
    }
    
    function generate_odd1( $main )
    {
        $data = array_map( 'intval', str_split( str_replace( ' ', '', $main ) ) );

        shuffle( $data );
        
        return [ $data[0], $data[1] ];
    }

    function generate_odd2( $main )
    {
        $data = array_map( 'intval', str_split( $main ) );

        shuffle( $data );
        
        return [ $data[0].$data[2], $data[4].$data[1], $data[3].$data[0] ];
    }
    
    function generate_shio( $key )
    {
        $shios = [ 1 => 'KERBAU', 2 => 'TIKUS', 3 => 'BABI', 4 => 'ANJING', 5 => 'AYAM', 6 => 'MONYET', 7 => 'KAMBING', 8 => 'KUDA', 9 => 'ULAR', 10 => 'NAGA', 11 => 'KELINCI', 12 => 'HARIMAU' ];
        $lists = [];
        $numb  = 1;

        for ($no = 1; $no <= 100; $no++) {
            if ($numb == 13) { $numb = 1; }
    
            $index = ($no == 100) ? '00' : ( $no <= 9 ? '0'.$no : $no );
            $lists[$index] = $shios[$numb];
    
            $numb++;
        }

        return $lists[$key];
    }
}
