<?php


require_once '../../../../../../includes/overall.inc.php';
require_once '../../../../../../includes/admin-functions.inc.php';

error_reporting(7);
ini_set('display_errors', true);
ini_set('html_errors', false);

global $L;


$smiles = 'bowtie,smile,laughing,blush,smiley,relaxed,smirk,heart_eyes,kissing_heart,kissing_closed_eyes,flushed,relieved,satisfied,grin,wink,stuck_out_tongue_winking_eye,stuck_out_tongue_closed_eyes,grinning,kissing,stuck_out_tongue,sleeping,worried,frowning,anguished,open_mouth,grimacing,confused,hushed,expressionless,unamused,sweat_smile,sweat,disappointed_relieved,weary,pensive,disappointed,confounded,fearful,cold_sweat,persevere,cry,sob,joy,astonished,scream,tired_face,angry,rage,triumph,sleepy,yum,mask,sunglasses,dizzy_face,imp,smiling_imp,neutral_face,no_mouth,innocent';

if ($smiles) {

$emoji_script = <<<HTML
	var text_last_emoji = "{$L['emoji_last']}";

function emojiFromHex(hex) {
	try {
		if ( navigator.platform.indexOf('Win') > -1 && hex.match( /^1F1(E[6-9A-F]|F[0-9A-F])/ ) ) {
			return null;
		}

		var decimals = [];
		var hexPoints = hex.split('-');
		for ( var p = 0; p < hexPoints.length; p++ ) {
			decimals.push( parseInt( hexPoints[p], 16 ) );
		}
		return String.fromCodePoint.apply( null, decimals );
	} catch ( err ) {
		return null;
	}
}

function getEmoji() {
	try {
        return JSON.parse(localStorage.getItem('last_emoji'));
    } catch (e) {
       return null;
    }
}

function setEmoji(value) {
    try {
        localStorage.setItem('last_emoji', JSON.stringify(value));
    } catch (e) {
    }
}

function inArray(needle, haystack){
	for (var i=0, len=haystack.length;i<len;i++) {
		if (haystack[i] == needle) return true;
	}
	return false;
}

function display_editor_last_emoji(){

	var emoji_array = getEmoji();
	var emoji = '';
	var div = '';

	if( $.isArray( emoji_array ) && emoji_array.length ) {

		div += '<div class="emoji_category"><b>'+text_last_emoji+'</b></div>';
		div += '<div class="emoji_list">';

		for (var i=0, len=emoji_array.length;i<len;i++) {
			emoji = emojiFromHex(emoji_array[i]);
			if(emoji) {
				div += '<div class="emoji_symbol" data-emoji="'+emoji_array[i]+'"><a onclick="insert_editor_emoji(\''+emoji+'\', \''+emoji_array[i]+'\'); return false;">'+emoji+'</a></div>';
			}
		}
		div += '</div>';
		
		divs = document.getElementsByClassName( 'last_emoji' );
		$('.last_emoji').html(div);
	}
}


function insert_editor_emoji(emoji, code) {

	parent.tinyMCE.execCommand('mceInsertContent',false,'<span class="native-emoji non-content-editable">'+emoji+'</span>');

	var emoji_array = getEmoji();
	if( $.isArray( emoji_array ) ) {
        if ( !inArray( code, emoji_array ) ) {
            if(emoji_array.length > 15 ) {
				emoji_array.pop();
			}
			emoji_array.unshift(code);
		}
	} else {
		emoji_array = [];
		emoji_array.push(code);
	}

	setEmoji(emoji_array);
	display_editor_last_emoji();
	parent.tinyMCE.activeEditor.windowManager.close();
}

$(function(){

	$(".emoji_box div[data-emoji]").each(function(){
		var code = $(this).data('emoji');
		var emoji = emojiFromHex($(this).data('emoji'));

		if(emoji) {
			$(this).html('<a onclick="insert_editor_emoji(\'' + emoji + '\', \'' + code + '\'); return false;">'+emoji+'</a>');
		} else {
			$(this).remove();
		}
	});

	display_editor_last_emoji();
});
HTML;


$output = <<<HTML
<div class="emoji_box"><div class="last_emoji"></div>
HTML;

	$emoji = json_decode(file_get_contents (GLOBAL_ROOT . "/admin/assets/emoticons/emoji.json" ) );

	foreach ($emoji as $key => $value ) {
		$i = 0;

		$output .= '<div class="emoji_category"><b>' . $L['emoji_'.$value->category] . '</b></div><div class="emoji_list">';
		foreach ($value->emoji as $character ) {
			$i++;
			$output .= '<div class="emoji_symbol" data-emoji="' . $character->code . '"></div>';
		}

		$output .= '</div>';
	}

$output .= '</div>';
	
} else {

    $globalUrl = GLOBAL_URL;
	$emoji_script = "";
	$i = 0;
	$output = '<table style="width:100%;border: 0px;padding: 0px;"><tr>';
	
$smiles = 'bowtie,smile,laughing,blush,smiley,relaxed,smirk,heart_eyes,kissing_heart,kissing_closed_eyes,flushed,relieved,satisfied,grin,wink,stuck_out_tongue_winking_eye,stuck_out_tongue_closed_eyes,grinning,kissing,stuck_out_tongue,sleeping,worried,frowning,anguished,open_mouth,grimacing,confused,hushed,expressionless,unamused,sweat_smile,sweat,disappointed_relieved,weary,pensive,disappointed,confounded,fearful,cold_sweat,persevere,cry,sob,joy,astonished,scream,tired_face,angry,rage,triumph,sleepy,yum,mask,sunglasses,dizzy_face,imp,smiling_imp,neutral_face,no_mouth,innocent';

    $smilies = explode(",", $smiles);
    $count_smilies = count($smilies);
    foreach($smilies as $smile)
    {
        $i++;
		$smile = trim($smile);
		$sm_image ="";
		if( file_exists( GLOBAL_ROOT . "/admin/assets/emoticons/" . $smile . ".png" ) ) {
			if( file_exists( GLOBAL_ROOT . "/admin/assets/emoticons/" . $smile . "@2x.png" ) ) {
				$sm_image = "<img alt=\"{$smile}\" class=\"emoji\" src=\"".GLOBAL_URL."/admin/assets/emoticons/{$smile}.png\" srcset=\"".GLOBAL_URL."/admin/assets/emoticons/{$smile}@2x.png 2x\" />";
			} else {
				$sm_image = "<img alt=\"{$smile}\" class=\"emoji\" src=\"".GLOBAL_URL."/admin/assets/emoticons/{$smile}.png\" />";
			}
		} elseif ( file_exists( GLOBAL_ROOT . "/admin/assets/emoticons/" . $smile . ".gif" ) ) {
			if( file_exists( GLOBAL_ROOT . "/admin/assets/emoticons/" . $smile . "@2x.gif" ) ) {
				$sm_image = "<img alt=\"{$smile}\" class=\"emoji\" src=\"". GLOBAL_URL. "admin/assets/emoticons/{$smile}.gif\" srcset=\"". GLOBAL_URL. "admin/assets/emoticons/{$smile}@2x.gif 2x\" />";
			} else {
				$sm_image = "<img alt=\"{$smile}\" class=\"emoji\" src=\"". GLOBAL_URL. "admin/assets/emoticons/{$smile}.gif\" />";
			}
		}
		
		$output .= "<td style=\"padding:5px;text-align: center;\"><a href=\"#\" onclick=\"smiley(':$smile:'); return false;\">{$sm_image}</a></td>";
		if ($i%7 == 0 AND $i < $count_smilies) $output .= "</tr><tr>";
	
	}

	$output .= "</tr></table>";
}

echo <<<HTML
<style>
	.emoji_box {
		width:100%;
		max-width: 390px;
	}
	.emoji_category {
		padding:7px;
		clear:both;
	}
	.emoji_list {
		margin-top:5px;
		margin-bottom:5px;
		width:100%;
		font-family:'Apple Color Emoji', 'Segoe UI Emoji', 'NotoColorEmoji', 'Segoe UI Symbol', 'Android Emoji', 'EmojiSymbols';
		font-size:1.5em;
	}
	.emoji_symbol {
		float:left;
		margin-bottom: 10px;
		width:12.5%;
		text-align:center;
	}
	
	.emoji_symbol a,  .emoji_symbol a:hover {
		cursor: pointer;
		text-decoration:none;
	}

	body {
		overflow-x:hidden;
		overflow-y:auto;
   }
</style>
<script src="/admin/assets/js/main/jquery.min.js"></script>
{$output}
<script>
<!--
    function smiley(finalImage) {
		parent.tinyMCE.execCommand('mceInsertContent', false, finalImage);
		parent.tinyMCE.activeEditor.windowManager.close();
	}
{$emoji_script}
-->
</script>
HTML;

?>