<?php

namespace App\Helper;

use App\Models\Lead;
use App\Models\Website;
use Illuminate\Support\Facades\DB;

/**
 * Class UtilHelper
 * @package App\Helper
 */
class Util
{
    const BOOL_FALSE = 0;
    const BOOL_TRUE  = 1;

    const GRAPH_MONTHLY = 0;
    const GRAPH_YEARLY  = 10;

    public static $BOOLS = [
        self::BOOL_FALSE => "No",
        self::BOOL_TRUE  => "Yes",
    ];

    public static $BOOLS_CSS = [
        self::BOOL_FALSE => "danger",
        self::BOOL_TRUE  => "success",
    ];

    public static $BOOLS_BG_CSS = [
        self::BOOL_FALSE => "red",
        self::BOOL_TRUE  => "green",
    ];

    /**
     * @param $value
     * @return mixed
     */
    public static function getBoolText($value)
    {
        return self::$BOOLS[$value];
    }

    /**
     * @param $value
     * @param bool $bg
     * @return mixed
     */
    public static function getBoolCss($value, $bg = false)
    {
        return $bg ? self::$BOOLS_BG_CSS[$value] : self::$BOOLS_CSS[$value];
    }

    public static function getDataTableParams()
    {
        return ['responsive' => true,];
    }

    public function readCSV($file, $colName = 0)
    {
        $row             = 0;
        $rows            = $columns = [];
        $autoLineEndings = ini_get('auto_detect_line_endings');
        ini_set('auto_detect_line_endings', TRUE);

        if (($handle = fopen(public_path() . '/csv/' . $file, "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
                if (count($data) <= 1) {
                    $columns[] = $data;
                    continue;
                }
                $row++;
                if ($row == 1) {
                    $columns[] = $data;
                    continue;
                }
                $rows[] = $data;
            }
            fclose($handle);
        }
        ini_set('auto_detect_line_endings', $autoLineEndings);

        if ($colName) {
            return [
                'rows'    => $rows,
                'columns' => $columns
            ];
        } else {
            return $rows;
        }
    }

    public function updateCSV($file, $data)
    {
        $current_content = $this->readCSV($file, 1);
        $new_data[0]     = $data;
        $new_content     = array_merge($current_content['columns'], $current_content['rows'], $data);

        $fp = fopen(public_path() . '/csv/' . $file, 'w');
        foreach ($new_content as $content) {
            fputcsv($fp, $content);
        }
        fclose($fp);

        return true;
    }

    public function seedWithCSV($file)
    {
        $newData = [];
        $data    = $this->readCSV($file, 1);

        foreach ($data['rows'] as $key => $row) {
            foreach ($row as $keys => $item) {
                $newData[$key][$data['columns'][0][$keys]] = $item;
            }
        }
        return $newData;
    }

    public static function getFromFirebaseRC()
    {
        $firebaserc = file_get_contents(url('/.firebaserc'), true);
        $firebaserc = (object)json_decode($firebaserc);
        return $firebaserc->firebase;
    }


    public static function getUrl($params, $delete = false)
    {
        $request = request();
        if ($delete) {
            foreach ($params as $param) {
                $request->query->remove($param);
            }
            $newQuery = $request->query();
        } else {
            $newQuery = array_merge($request->query(), $params);
        }
        return $request->fullUrlWithQuery($newQuery);
    }

    public static function getFilterByUser()
    {
        $audio_only = Util::getUrl(['user_id' => \Auth::id()]);
        return [
            [
                "extend"  => "collection",
                "text"    => "<i class='fa fa-filter'></i> MyLeads",
                "buttons" => [
                    [
                        "name"   => "audio",
                        "text"   => "<i class='fa fa-volume-up'></i> Only My",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $audio_only . "' }"
                    ],
                ]
            ]
        ];
    }

    public static function getFilterByPlatform()
    {

        $pf1 = Util::getUrl(['platform' => 'Manual']);
        $pf2 = Util::getUrl(['platform' => 'Social Media']);
        $pf3 = Util::getUrl(['platform' => 'Ppc']);
        $pf4 = Util::getUrl(['platform' => 'Referral']);
        return [
            [
                "extend"  => "collection",
                "text"    => "<i class='fa fa-filter'></i> Platform",
                "buttons" => [
                    [
                        "name"   => "Manual",
                        "text"   => "Manual",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $pf1 . "'}"
                    ],
                    [
                        "name"   => "Social Media",
                        "text"   => "Social Media",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $pf2 . "'}"
                    ],
                    [
                        "name"   => "Ppc",
                        "text"   => "Ppc",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $pf3 . "'}"
                    ],
                    [
                        "name"   => "Referral",
                        "text"   => "Referral",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $pf4 . "'}"
                    ],
                ]
            ]
        ];
    }


    public static function getFilterByMonth()
    {

        $months  = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
        $month   = Util::getUrl(['month' => '01']);
        $month1  = Util::getUrl(['month' => '02']);
        $month2  = Util::getUrl(['month' => '03']);
        $month3  = Util::getUrl(['month' => '04']);
        $month4  = Util::getUrl(['month' => '05']);
        $month5  = Util::getUrl(['month' => '06']);
        $month6  = Util::getUrl(['month' => '07']);
        $month7  = Util::getUrl(['month' => '08']);
        $month8  = Util::getUrl(['month' => '09']);
        $month9  = Util::getUrl(['month' => '10']);
        $month10 = Util::getUrl(['month' => '11']);
        $month11 = Util::getUrl(['month' => '12']);
        foreach ($months as $key => $val) {
            $a[$key] = "<i>$val</i>";
        }
        return [
            [
                "extend"  => "collection",
                "text"    => "<i class='fa fa-calendar'></i> Months",
                "buttons" => [
                    [
                        "name"   => "Jan",
                        "text"   => "$a[0]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month . "'}"
                    ],
                    [
                        "name"   => "Feb",
                        "text"   => "$a[1]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month1 . "' }"
                    ],
                    [
                        "name"   => "Mar",
                        "text"   => "$a[2]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month2 . "' }"
                    ],
                    [
                        "name"   => "Apr",
                        "text"   => "$a[3]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month3 . "' }"
                    ],
                    [
                        "name"   => "May",
                        "text"   => "$a[4]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month4 . "' }"
                    ],
                    [
                        "name"   => "Jun",
                        "text"   => "$a[5]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month5 . "' }"
                    ],
                    [
                        "name"   => "July",
                        "text"   => "$a[6]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month6 . "' }"
                    ],
                    [
                        "name"   => "Aug",
                        "text"   => "$a[7]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month7 . "' }"
                    ],
                    [
                        "name"   => "Sept",
                        "text"   => "$a[8]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month8 . "' }"
                    ],
                    [
                        "name"   => "Oct",
                        "text"   => "$a[9]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month9 . "' }"
                    ],
                    [
                        "name"   => "Nov",
                        "text"   => "$a[10]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month10 . "' }"
                    ],
                    [
                        "name"   => "Dec",
                        "text"   => "$a[11]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month11 . "' }"
                    ]

                ]

            ]
        ];


    }


    public static function getFilterByYear()
    {

        $months = ['2020', '2021', '2022', '2023','2024','2025'];
        $month  = Util::getUrl(['year' => '2020']);
        $month1 = Util::getUrl(['year' => '2021']);
        $month2 = Util::getUrl(['year' => '2022']);
        $month3 = Util::getUrl(['year' => '2023']);
        $month4 = Util::getUrl(['year' => '2024']);
        $month5 = Util::getUrl(['year' => '2025']);
        foreach ($months as $key => $val) {
            $a[$key] = "<i>$val</i>";
        }
        return [
            [
                "extend"  => "collection",
                "text"    => "<i class='fa fa-calendar'></i> Year",
                "buttons" => [
                    [
                        "name"   => "Jan",
                        "text"   => "$a[0]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month . "'}"
                    ],
                    [
                        "name"   => "Feb",
                        "text"   => "$a[1]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month1 . "' }"
                    ],
                    [
                        "name"   => "Mar",
                        "text"   => "$a[2]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month2 . "' }"
                    ],
                    [
                        "name"   => "Apr",
                        "text"   => "$a[3]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month3 . "' }"
                    ], [
                        "name"   => "Apr",
                        "text"   => "$a[4]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month4 . "' }"
                    ],[
                        "name"   => "Apr",
                        "text"   => "$a[5]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month5 . "' }"
                    ]

                ]

            ]
        ];


    }
    public static function getFilterByType()
    {

        $months = ['Certification', 'Academic Exam', 'Academic Class', 'Others'];
        $month  = Util::getUrl(['type' => '10']);
        $month1 = Util::getUrl(['type' => '20']);
        $month2 = Util::getUrl(['type' => '30']);
        $month3 = Util::getUrl(['type' => '40']);
        foreach ($months as $key => $val) {
            $a[$key] = "<i>$val</i>";
        }
        return [
            [
                "extend"  => "collection",
                "text"    => "<i class='fa fa-address-card'></i> Lead Type",
                "buttons" => [
                    [
                        "name"   => "Jan",
                        "text"   => "$a[0]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month . "'}"
                    ],
                    [
                        "name"   => "Feb",
                        "text"   => "$a[1]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month1 . "' }"
                    ],
                    [
                        "name"   => "Mar",
                        "text"   => "$a[2]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month2 . "' }"
                    ],
                    [
                        "name"   => "Apr",
                        "text"   => "$a[3]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month3 . "' }"
                    ]

                ]

            ]
        ];


    }
    public static function getFilterByPaymentType()
    {

        $months = ['Fresh', 'UpSell', 'Existing'];
        $month  = Util::getUrl(['pay_type' => '4']);
        $month1 = Util::getUrl(['pay_type' => '5']);
        $month2 = Util::getUrl(['pay_type' => '6']);
        foreach ($months as $key => $val) {
            $a[$key] = "<i>$val</i>";
        }
        return [
            [
                "extend"  => "collection",
                "text"    => "<i class='fa fa-certificate '></i> Payment Type",
                "buttons" => [
                    [
                        "name"   => "Jan",
                        "text"   => "$a[0]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month . "'}"
                    ],
                    [
                        "name"   => "Feb",
                        "text"   => "$a[1]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month1 . "' }"
                    ],
                    [
                        "name"   => "Mar",
                        "text"   => "$a[2]",
                        "action" => "function(e, dt, button, config) { window.location.href='" . $month2 . "' }"
                    ]

                ]

            ]
        ];


    }

}