<?php

// Code within app\Helpers\Helper.php

namespace App\Helpers;

use Config;
use App\Settings;
use Mail; 
class Functions {

    public static function replaces($data, $replaces) {

        foreach ($replaces as $key => $replace) {
            $data['body'] = str_replace("[" . $key . "]", $replace, $data['body']);
            $data['title'] = str_replace("[" . $key . "]", $replace, $data['title']);
            $data['subject'] = str_replace("[" . $key . "]", $replace, $data['subject']);
        }

        return $data;
    }
    
    public static function rawAmount($amount) {
        $float_amount = (float) $amount;
        $exponent = 2;
        $int_amount = (int) round($float_amount * pow(10, $exponent));
        return (string) $int_amount;
    }

    public static function getTimeRanges() {

        for ($i = 0; $i < 24; $i++) {

            $j = $i + 1;
            $time = sprintf('%02d', $i) . ":00";
            //$time = date("h:i a", strtotime($time));

            $time2 = sprintf('%02d', $j) . ":00";
            //$time2 = date("h:i a", strtotime($time2));
            if ($time2 == "24:00") {
                $time2 = "00:00";
            }

            $range[$time . "-" . $time2] = date("h:i a", strtotime($time)) . "-" . date("h:i a", strtotime($time2));
        }
        return $range;
    }

    public static function getSettings() {

        $model = Settings::get();

        $settings = array();
        foreach ($model as $row) {
            $settings[$row->Title] = $row->Content;
        }
        return $settings;
    }
    
    public static function sendEmail($subject,$email,$name,$templateName,$customers,$rand_token) {
        
            $data['user'] =$customers;
            $data['token']=$rand_token;
            $data['subject'] = $subject;
            $data['name'] = $name;
            $data['email'] = $email;
            Mail::send($templateName, $data, function ($m) use ($data) {
                $m->from(env("MAIL_FROM_ADDRESS"), env("MAIL_FROM_NAME"));
                $m->to($data['email'], $data['name'])->subject($data['subject']);
                                          
            });

       
    }
    public static function sendEmailFun($email,$subject,$msg){
        mail($email,$subject,$msg);
    }

     public static function sendMailChimp($subject,$to_name,$content){
        
        $listId = env('MAILCHIMP_LIST_ID');

        $mailchimp = new \Mailchimp(env('MAILCHIMP_KEY'));
        
                    
        
        $campaign = $mailchimp->campaigns->create('regular', [
            'list_id' => $listId,
            'subject' => $subject,
            'from_email' => 'sindhoo.aziz164@gmail.com',
            'from_name' => 'Rajesh',
            'to_name' => $to_name

        ], [
            'html' => $content,
            'text' => strip_tags($content)
        ]);

        //Send campaign
        $mailchimp->campaigns->send($campaign['id']);
        

    }
}
