<?php
/**
 * Created by PhpStorm.
 * User: mruongyutthy
 * Date: 1/5/20
 * Time: 22:05
 */

namespace App\Helpers;


use DateTime;
use Illuminate\Support\Facades\App;

use Illuminate\Support\Facades\Auth;
use App\Models\Sample;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;


class Helpers
{
    public static function lang()
    {
        return App::getLocale();
    }

    public static function numberFormat($value, $decimal = null)
    {
        if (empty($value) && $value != 0) {
            return '';
        }
        if ($decimal > 0) {
            return number_format($value, $decimal);
        }

        return number_format($value, fmod($value, 1) == 0 ? 0 : 2);
    }

    public static function isDateValid($date)
    {
        if(!is_null($date) && $date != '' && !empty($date) && $date !== "0000-00-00") {
            return ((strtotime($date) < 0) || (strtotime($date) == false)) ? false : true;
        }
        else return false;
    }

    public static function generateUuId($prefixClass){
        $hexTimestamp = strtoupper(dechex(round((1000000*gettimeofday()['sec']) + (gettimeofday()['usec']))));
        return
            $prefixClass .
            '-'.
            str_pad(substr($hexTimestamp, 8, 3),3,'0',STR_PAD_RIGHT).
            str_pad(substr($hexTimestamp, 11, 3),3,'0',STR_PAD_LEFT).
            str_pad(mt_rand(1,99),2,'0',STR_PAD_LEFT);
    }
    
    public static function generateUuIdForManual($prefixClass, $manual_code){
        return $prefixClass . '-'.date('ymd').'-'.$manual_code;
    }

    public static function split_name($name) {
        return explode(" ",$name,2);
    }

    public static function getAge($startDate, $endDate) {
        $today = new DateTime($endDate);
        $diff = $today->diff(new DateTime($startDate));
        $years = $diff->y;
        $months = $diff->m;
        $days = $diff->d;

        /*if($years>0) return "$years ".__('sample.year') .($months > 0 ? ' '. $months .__('sample.month') : '') . ($days>0? ' ' . $days . __('sample.day'): '');
        return ($months > 0 ? ' '. $months .__('sample.month') : ''). ($days>0? ' ' . $days . __('sample.day'): '');*/

        if($years ==0 && $months == 0) return $days . __('sample.day');
        elseif($years == 0 && $months > 0) return $months .__('sample.month');
        else return "$years ".__('sample.year');

        /*if($years>0) return "$years ".__('sample.year') .($months > 0 ? ' '. $months .__('sample.month') : '');
        return $months .__('sample.month') .', '. $days . __('sample.day');*/
    }

    public static function getGender($sex){
        if($sex == 1) return __('sample.sex_m');
        return __('sample.sex_f');
    }
	
	public static function getGenderfull($sex){
        if($sex == 1) return __('sample.sex_male');
        return __('sample.sex_female');
    }

    public static function knum($string){
        $string = str_replace(array('1','2','3','4','5','6','7','8','9','0','.'),
            array('១','២','៣','៤','៥','៦','៧','៨','៩','០',','), $string);
        return $string;
    }

    public static function getPatientAddress($patientObject){
        $address = '';
        if(App::getLocale()=='en'){
            if(isset($patientObject->province->name_en)) $address.= $patientObject->province->name_en;
            if(isset($patientObject->district->name_en)) $address.= ', '. $patientObject->district->name_en;
            if(isset($patientObject->commune->name_en)) $address.= ', '. $patientObject->commune->name_en;
            if(isset($patientObject->viilage->name_en)) $address.= ', '. $patientObject->viilage->name_en;
        }else{
            if(isset($patientObject->province->name_kh)) $address.= $patientObject->province->name_kh;
            if(isset($patientObject->district->name_kh)) $address.= ', '. $patientObject->district->name_kh;
            if(isset($patientObject->commune->name_kh)) $address.= ', '. $patientObject->commune->name_kh;
            if(isset($patientObject->viilage->name_kh)) $address.= ', '. $patientObject->viilage->name_kh;
        }
        return $address;
    }

    public static function getPrinterSignature($sampleId){
        $sample = Sample::query()->find( $sampleId);
        if($sample->is_printed == 1){
            return User::query()->find($sample->printed_by)->signature;
        }else{
            return @Auth::user()->signature;
        }
    }

    public static function setExcelSheetCell($sheet, $cellStart, $cellEnd, $value, $format = []){
        if($cellStart != $cellEnd){
            $sheet->mergeCells($cellStart.":".$cellEnd);
        }
        foreach(range(Str::substr($cellStart,0,1),Str::substr($cellEnd,0,1)) as $columnID) {
            $sheet->getColumnDimension($columnID)->setAutoSize(true);
        }
        $sheet->setCellValue($cellStart, $value);
        if(!is_null($format) || !empty($format)){
            $sheet->getStyle($cellStart.":".$cellEnd)->applyFromArray($format);
        }
    }

    public static function getNotify(){
        $publication = DB::select("
        SELECT
            p.*
        FROM publications p
        INNER JOIN publication_receivers pr
        ON pr.publication_id = p.id
        WHERE DATE(NOW()) BETWEEN start_date AND end_date AND p.record_status_id = 1
        AND pr.lab_id = " . Session::get('base_lab_id'));
        return $publication;
    }
    
    public static function generatePatientBotLink($individual_type = 'patient',$labId, $patientId)
    {
        $botUsername = env('TELEGRAM_BOT_NAME'); // e.g. "MyLabBot"
        if ($individual_type == 'sample_source'){
            // if $individual_type is sample_source, patientId is sample source id
            return "https://t.me/{$botUsername}?start=samplesource_{$patientId}";
        }
        return "https://t.me/{$botUsername}?start={$patientId}";
    }

}
