package cn.hmsoft.frame.control;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.application.web.Ajax;
import cn.hmsoft.application.web.AjaxControl;
import cn.hmsoft.frame.constants.FrameStatus;
import cn.hmsoft.frame.constants.FrameThreadCallbackType;
import cn.hmsoft.frame.constants.FrameThreadStatus;
import cn.hmsoft.frame.data.dao.FrameThreadDao;
import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.data.model.FrameThread;
import cn.hmsoft.frame.thread.FrameThreadHelper;
import cn.hmsoft.jdbc.entity.DataRecord;

@RestController
public class FrameThreadControl extends AjaxControl {

	@Autowired
	private FrameThreadDao daoThread;

	@RequestMapping("frame/thread/page")
	public Ajax page(String query, Integer limit, Integer start, String order, String type) {
		return new Ajax(this.daoThread.pageThread(start, limit, query, getQueryOrder(order, type)));
	}

	@RequestMapping("frame/thread/delete")
	public Ajax delete(final int thread_id) {
		this.daoThread.delete(thread_id);
		return new Ajax();
	}

	@RequestMapping("frame/thread/find")
	public Ajax find(int thread_id) {
		return new Ajax(this.daoThread.find(thread_id));
	}

	/*********************
	 * 测试线程用
	 */
	@RequestMapping("frame/thread/test")
	public Ajax testThread(FrameThread thread, String error_flag) throws Exception {
		FrameOptr optr = new FrameOptr();
		optr.setOptr_id(0);
		optr.setOptr_name("Admin");
		optr.setLogin_name("admin");

		FrameThreadCallbackType type = FrameThreadCallbackType.Download;
		if (FrameThreadCallbackType.Swal.toString().equals(thread.getCallback_type()))
			type = FrameThreadCallbackType.Swal;
		FrameThread run = FrameThreadHelper.createThread(thread.getThread_name(), thread.getThread_notice(), type,
				thread.getCallback_param(), optr);
		new Thread(() -> {
			FrameThreadHelper.updateThread(run, 1, "后台线程开始运行！", FrameThreadStatus.Running);
			for (int i = 1; i < 10; i++) {
				if (error_flag.equals(FrameStatus.Active.toString()) && i == 6) {
					// 模拟错误信息
					FrameThreadHelper.completeThread(run, "线程运行出现错误！", FrameThreadCallbackType.None,
							"后台线程未正常结束，这里将展示错误提示！", FrameThreadStatus.Error);
					return;
				}
				FrameThreadHelper.updateThread(run, 1 + i * 10, "第【" + i + "】次更新线程运行信息！");
				FrameThreadHelper.sleepSeconds(5);
			}
			if (FrameThreadCallbackType.Download.toString().equals(thread.getCallback_type())) {
				// 下载文件
				FrameThreadHelper.completeThread(run, "所需文件已经准备完毕，请注意允许浏览器打开新网页！", FrameThreadCallbackType.Download,
						FrameThreadHelper.generateDownloadParam(
								new File(SpringHelper.getRootPath() + "template" + File.separator + "ThreadTest.txt"),
								"线程测试文件"));
			} else if (FrameThreadCallbackType.Swal.toString().equals(thread.getCallback_type())) {
				FrameThreadHelper.completeThread(run, "后台线程运行完毕！", FrameThreadCallbackType.Swal, "这是回调函数 -> 弹出框");
			} else {
				FrameThreadHelper.completeThread(run, "后台线程运行完毕！无其他多余操作", FrameThreadCallbackType.None, null);
			}
		}).start();
		Ajax ajax = new Ajax();
		ajax.setEntity(run);
		ajax.setThreadId(run.getThread_id());
		return ajax;
	}

	/*********************
	 * 测试文件上传
	 */
	@RequestMapping("frame/upload/test")
	public Ajax testUpload(DataRecord dr, MultipartFile file) throws Exception {
		System.out.println(dr.getS01());
		return new Ajax();
	}

	/*********************
	 * 测试文件上传
	 */
	@RequestMapping("frame/upload/thread/test")
	public Ajax testUploadWithThread(DataRecord dr, MultipartFile file) throws Exception {
		FrameOptr optr = new FrameOptr();
		optr.setOptr_id(0);
		optr.setOptr_name("Admin");
		optr.setLogin_name("admin");
		
		FrameThread run = FrameThreadHelper.createThread("框架文件上传测试", "上传后后台线程处理", FrameThreadCallbackType.Download,
				null, optr);
		new Thread(() -> {
			FrameThreadHelper.updateThread(run, 1, "后台线程开始运行！", FrameThreadStatus.Running);
			for (int i = 1; i < 10; i++) {
				FrameThreadHelper.updateThread(run, 1 + i * 10, "第【" + i + "】次更新线程运行信息！");
				FrameThreadHelper.sleepSeconds(5);
			}
			// 下载文件
			FrameThreadHelper.completeThread(run, "所需文件已经准备完毕，请注意允许浏览器打开新网页！", FrameThreadCallbackType.Download,
					FrameThreadHelper.generateDownloadParam(
							new File(SpringHelper.getRootPath() + "template" + File.separator + "ThreadTest.txt"),
							"线程测试文件"));
		}).start();
		
		Ajax ajax = new Ajax();
		ajax.setEntity(run);
		ajax.setThreadId(run.getThread_id());
		return ajax;
	}

}
