package com.uas.search.console.b2b.controller;

import java.io.IOException;
import java.io.NotSerializableException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.uas.search.b2b.service.SearchService.Table_name;
import com.uas.search.console.b2b.Application;
import com.uas.search.console.b2b.SystemProperties;
import com.uas.search.console.b2b.schedule.model.TaskInformation;
import com.uas.search.console.b2b.schedule.service.TaskService;
import com.uas.search.console.b2b.service.UpdateVirtualColumnService;
import com.uas.search.console.b2b.util.ClassAndTableNameUtils;

@Controller
@RequestMapping("/schedule")
public class ScheduleController {

	@Autowired
	private SystemProperties systemProperties;

	@Autowired
	private UpdateVirtualColumnService updateVirtualColumnService;

	@Autowired
	private TaskService taskService;

	@RequestMapping("/updateVirtualColumns")
	@ResponseBody
	public String updateVirtualColumns(@RequestParam String tableName,
			@RequestParam String fields, @RequestParam Integer hour,
			@RequestParam Integer minute, @RequestParam Integer second)
			throws ClassNotFoundException, NotSerializableException, IOException {
		Table_name table = Table_name.valueOf(tableName.toUpperCase());
		if (table != Table_name.PURC$INQUIRYMOULD && table != Table_name.PURC$INQUIRYITEMS
				&& table != Table_name.PURC$TENDER && table != Table_name.SALE$QUOTATION) {
			return "不支持更新虚拟列索引：" + tableName;
		}
		Map<String, Class<?>> map = new HashMap<>();
		JSONObject jsonObject = JSONObject.parseObject(fields);
		Set<Entry<String, Object>> entrySet = jsonObject.entrySet();
		for (Entry<String, Object> entry : entrySet) {
			map.put(entry.getKey(), Class.forName((String) entry.getValue()));
		}
		String message = "已开启：" + updateVirtualColumnService.newDailyTask(ClassAndTableNameUtils.toClass(table), map,
				hour, minute, second);
		restart();
		return message;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public List<TaskInformation> delete(@RequestParam List<String> titles) {
		return taskService.delete(titles);
	}

	@RequestMapping("/deleteAll")
	@ResponseBody
	public List<TaskInformation> deleteAll() {
		return taskService.deleteAll();
	}

	@RequestMapping("/all")
	@ResponseBody
	public List<TaskInformation> all() {
		return taskService.all();
	}

	@RequestMapping("/start")
	@ResponseBody
	public String start() {
		return taskService.start();
	}

	@RequestMapping("/stop")
	@ResponseBody
	public String stop() {
		return taskService.stop();
	}

	@RequestMapping("/restart")
	@ResponseBody
	public String restart() {
		if (!taskService.isStopped()) {
			taskService.stop();
		}
		return taskService.start();
	}

	@RequestMapping("/isStopped")
	@ResponseBody
	public boolean isStopped() {
		return taskService.isStopped();
	}

	@RequestMapping("/init")
	@ResponseBody
	public List<TaskInformation> init(Integer hour, Integer minute, Integer second)
			throws NotSerializableException, ClassNotFoundException, IOException {
		if (hour == null) {
			hour = systemProperties.getDailyTaskHour();
		}
		if (minute == null) {
			minute = systemProperties.getDailyTaskMinute();
		}
		if (second == null) {
			second = systemProperties.getDailyTaskSecond();
		}
		return Application.startTask(hour, minute, second);
	}
}
