/*******************************************************************************
 * 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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import com.ffii.core.utils.CriteriaUtils;
import com.ffii.core.utils.MapUtils;
import com.ffii.core.utils.Params;
import com.ffii.core.utils.StringUtils;
import com.ffii.core.web.AbstractController;
import com.ffii.core.web.view.json.JsonView;
import com.ffii.tbms.file.File;
import com.ffii.tbms.file.service.FileService;
import com.ffii.tbms.order.Order;
import com.ffii.tbms.order.service.OrderService;

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

	@Autowired
	private FileService fileService;

	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/list.json", method = { RequestMethod.GET, RequestMethod.POST })
	public String listJson(Model model, HttpServletRequest request, @RequestParam(required = false) String refType,
	 @RequestParam(required = false) Integer refId)	throws ServletRequestBindingException {

		Map<String, Object> args = new HashMap<String, Object>();
		CriteriaUtils.addDate(request, args, "startDate");
		CriteriaUtils.addDateTo(request, args, "endDate");

		List<Map<String,Object>> records = new ArrayList<>();
		
		String documentRefType = ServletRequestUtils.getStringParameter(request, "documentRefType");
		Integer documentRefId = ServletRequestUtils.getIntParameter(request, "documentRefId");
		if (StringUtils.isNotEmpty(documentRefType) && documentRefId != null) {
			args.put("documentRefType", documentRefType);
			args.put("documentRefId", documentRefId);

			records = fileService.searchFilesFromDocument(args);
		} else if (StringUtils.isNotEmpty(refType) && refId != null) {
			args.put(Params.REF_TYPE, refType);
			args.put(Params.REF_ID, refId);

			records = fileService.searchFiles(args);
		}

		model.addAttribute(Params.RECORDS, records);

		return JsonView.NAME;
	}

	@RequestMapping(value = "/list-order.json", method = { RequestMethod.GET, RequestMethod.POST })
	public String listOrderJson(Model model, HttpServletRequest request, @RequestParam(required = false) String refType,
	 @RequestParam(required = false) Integer refId)	throws ServletRequestBindingException {

		Map<String, Object> args = new HashMap<String, Object>();
		List<Map<String,Object>> records = new ArrayList<>();
			args.put("refType", refType);
			args.put("refId", refId);
			records = fileService.searchFiles(args);

			Order order = orderService.find(refId);

			if(order.getFileId() != null)
				records.addAll(fileService.searchFiles(MapUtils.toHashMap("fileId",order.getFileId())));

		model.addAttribute(Params.RECORDS, records);
		model.addAttribute("title", order.getType()+order.getCode());

		return JsonView.NAME;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Model model, HttpServletResponse response,
			@RequestParam int id, @RequestParam String skey, @RequestParam String filename,
			@RequestParam(defaultValue = StringUtils.EMPTY) String description) {

		File file = fileService.findFileByIdAndKey(id, skey);

		if (file != null) {

			file.setFilename(filename);
			file.setDescription(description);
			fileService.saveFile(file);

			model.addAttribute(Params.SUCCESS, Boolean.TRUE);
		} else {
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}

		return JsonView.NAME;
	}

}
