package com.neusoft.smart.pos.org.service;

import com.neusoft.smart.pos.dto.org.request.TableWithDishesRequest;
import com.neusoft.smart.pos.framework.utils.BeanUtil;
import com.neusoft.smart.pos.redis.bean.TablePrintBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
@Deprecated
public class PrintService {
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	/**
	 * @param shopId
	 * @param menuList
	 */
	public void insertToQueue(Integer shopId, List<TableWithDishesRequest.Menu> menuList) {
		BoundValueOperations<String, TablePrintBean> valueOps = redisTemplate.boundValueOps("printQueue_" + shopId + "_" + UUID.randomUUID());
		TablePrintBean tablePrintBean = new TablePrintBean();
		List<TablePrintBean.Menu> insertMenus = new ArrayList<>();
		menuList.forEach(menu -> {
			tablePrintBean.setTableName(menu.getTableName());
			TablePrintBean.Menu innerMenu = new TablePrintBean.Menu();
			innerMenu.setCategory(menu.getCategory());
			innerMenu.setName(menu.getName());
			innerMenu.setSelectedFlavors(BeanUtil.parseList(menu.getSelectedFlavors(), TablePrintBean.Flavor.class));
			innerMenu.setCount(menu.getCount());
			insertMenus.add(innerMenu);
		});
		tablePrintBean.setMenuList(insertMenus);
		valueOps.set(tablePrintBean);
	}
	
	/**
	 * @param shopId
	 * @return
	 */
	public synchronized List<TablePrintBean> popMenus(Integer shopId) {
		Set<String> keySet = redisTemplate.keys("printQueue_" + shopId + "_*");
		List<TablePrintBean> printList = new ArrayList<>();
		keySet.forEach(key -> {
			TablePrintBean insertMenus = (TablePrintBean) redisTemplate.boundValueOps(key).get();
			printList.add(insertMenus);
		});
		redisTemplate.delete(keySet);
		return printList;
	}
}
