<?php

if($action == "editKeyboard" && $text == "cancel"){
	$reply = "Session had been cancelled. Thank you";
	$keyboard = '{"keyboard":[],"remove_keyboard":true}';
	
	mysqli_query($conn,"DELETE FROM telegram_bot WHERE tb_chat_id='$chatID'");
	mysqli_query($conn,"DELETE FROM telegram_session WHERE ts_chat_id='$chatID'");
	mysqli_query($conn,"DELETE FROM telegram_cache WHERE tc_chat_id='$chatID'");
	sendMessage($chatID,$reply,$keyboard);
	
	exit();
}

if($action == "editKeyboard" && $text == "Add more row"){
	
	$sessionResult = mysqli_query($conn,"SELECT * FROM telegram_session WHERE ts_chat_id='$chatID'");
	$sessionRow = mysqli_fetch_assoc($sessionResult);
	$row = $sessionRow['ts_session'];
	
	$row++;
	mysqli_query($conn,"UPDATE telegram_session SET ts_session=$row WHERE ts_chat_id='$chatID'");
	
	$reply = "Your response?";
	$keyboard = '{"keyboard":
		[
			[{"text":"1x1"},{"text":"2x1"},{"text":"3x1"},{"text":"4x1"},{"text":"5x1"}],
			[{"text":"Kb"}]
		],
		"one_time_keyboard":false,"resize_keyboard":true}';
	sendMessage($chatID,$reply,$keyboard);
	exit();
}

if($action == "editKeyboard"){
	$sessionResult = mysqli_query($conn,"SELECT * FROM telegram_session WHERE ts_chat_id='$chatID'");
	$sessionRow = mysqli_fetch_assoc($sessionResult);
	$row = $sessionRow['ts_session'];
	
	switch($text){
		case "1x1" :
			$grid = 1;
			mysqli_query($conn,"UPDATE telegram_session SET ts_note=$grid, ts_session=$row WHERE ts_chat_id='$chatID'");
			$reply = "Your response?"; sendMessage($chatID,$reply);
			exit();
		case "5x1" : 
			$grid = 5;
			mysqli_query($conn,"UPDATE telegram_session SET ts_note=$grid, ts_session=$row WHERE ts_chat_id='$chatID'");
			$reply = "Your response?"; sendMessage($chatID,$reply);
			exit();
	}
	
	if($sessionRow['ts_note'] > 0){
		$row = $sessionRow['ts_session'];
		mysqli_query($conn,"INSERT INTO telegram_cache (tc_cache,tc_row,tc_chat_id) VALUES ('$text',$row,'$chatID')");
		
		$updateSession = $sessionRow['ts_note'] - 1; 
		//sendMessage($chatID,$updateSession);
		mysqli_query($conn,"UPDATE telegram_session SET ts_note='$updateSession' WHERE ts_chat_id='$chatID'");
	}
	
	if($updateSession == 0){
		$cacheResult = mysqli_query($conn,"SELECT * FROM telegram_cache WHERE tc_chat_id='$chatID' ORDER BY tc_id ASC");
		$no = 0;
		while($cacheRow = mysqli_fetch_assoc($cacheResult)){
			$cache = $cacheRow['tc_cache'];
			//$row = $cacheRow['tc_row'];
			//sendMessage($chatID,$cache); 
			$no++; if($no==1){$comma = "";}else{$comma = ",";}
			$presentRow = $cacheRow['tc_row'];
			if($prevRow < $presentRow){$comma="],[";}
			$keyboardMarkupCode = $keyboardMarkupCode.$comma.'{"text":"'.$cache.'"}';
			$prevRow = $presentRow;
		}
		
		$keyboard = '{"keyboard":[['.$keyboardMarkupCode.'],
		[{"text":"Add more row"}],[{"text":"Done"}]],"one_time_keyboard":false,"resize_keyboard":true}';
		sendMessage($chatID,$keyboardMarkupCode,$keyboard);
		
		//mysqli_query($conn,"DELETE FROM telegram_session WHERE ts_chat_id='$chatID'");
		//mysqli_query($conn,"DELETE FROM telegram_cache WHERE tc_chat_id='$chatID'");
	}
}

?>