/*******************************************************************************
 * Copyright 2019 2Fi Business Solutions Ltd.
 *
 *
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.tbms.file.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.ffii.core.utils.MapUtils;
import com.ffii.core.utils.NumberUtils;
import com.ffii.core.utils.Params;
import com.ffii.core.utils.web.ServletRequestUtils;
import com.ffii.core.web.AbstractController;
import com.ffii.core.web.view.json.JsonView;
import com.ffii.tbms.file.FileRefType;
import com.ffii.tbms.file.service.FileService;
import com.ffii.tbms.log.LogUtils;
import com.ffii.tbms.order.service.OrderService;

@Controller
@RequestMapping(value = "/file")
public class DeleteFileController extends AbstractController {

	@Autowired
	private FileService fileService;

	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/delete", method = { RequestMethod.GET, RequestMethod.POST })
	public String delete(Model model,
			@RequestParam Integer fileId, @RequestParam Integer refId, @RequestParam String refType, @RequestParam String skey)
			throws Exception {

		fileService.deleteFile(fileId, refId, refType, skey);

		if(refType.equals(FileRefType.MEASUREMENT_SHEET)){
			orderService.log(LogUtils.ACTION_DELETE,
				refId,
				MapUtils.toHashMap("fileId", fileId)
			);
		}else if(refType.equals(FileRefType.ORDER)){
			orderService.log(LogUtils.ACTION_DELETE,
				refId,
				MapUtils.toHashMap("photoFileId", fileId)
			);
		}

		model.addAttribute(Params.SUCCESS, Boolean.TRUE);

		return JsonView.NAME;
	}

	@RequestMapping(value = "/multi-delete", method = { RequestMethod.GET, RequestMethod.POST })
	public String multiDelete(Model model, final HttpServletRequest request)
			throws Exception {
				
		@SuppressWarnings("unchecked")
		List<Map<String,Object>> deleteRecord = 
		(List<Map<String, Object>>)ServletRequestUtils.getJsonListParameter(request, "deleteRecord");

		for(Map<String, Object> record: deleteRecord){
			int fileId = NumberUtils.intValue(record.get("fileId"));
			int refId = NumberUtils.intValue(record.get("refId"));
			String refType = record.get("refType").toString();
			String skey = record.get("skey").toString();
			
			fileService.deleteFile(fileId, refId, refType, skey);

			if(refType.equals(FileRefType.MEASUREMENT_SHEET)){
				orderService.log(LogUtils.ACTION_DELETE,
					refId,
					MapUtils.toHashMap("fileId", fileId)
				);
			}else if(refType.equals(FileRefType.ORDER)){
				orderService.log(LogUtils.ACTION_DELETE,
					refId,
					MapUtils.toHashMap("photoFileId", fileId)
				);
			}
		}
		model.addAttribute(Params.SUCCESS, Boolean.TRUE);

		return JsonView.NAME;
	}

}
