package com.nokia.tsl_data.controller;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.springframework.scheduling.support.CronExpression;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nokia.common.http.R;
import com.nokia.tsl_data.service.TslCronTaskService;
import com.nokia.tsl_data.service.TslTaskService;
import com.nokia.tsl_data.service.TslWaraHouseService;

/**
 * 投诉率相关数据源入库
 * 
 * 1. 支持定时任务状态查询、修改定时任务表达式、启动或停止定时任务
 * 支持手动重新生成某天的报告或入库
 */
@RequestMapping("/tsl/task")
@RestController
public class TslTaskConroller {

    private final TslTaskService taskService;
    private final TslCronTaskService cronTaskService;
    private final TslWaraHouseService tslWaraHouseService;

    public TslTaskConroller(TslTaskService taskService, TslCronTaskService cronTaskService,
            TslWaraHouseService tslWaraHouseService) {
        this.taskService = taskService;
        this.cronTaskService = cronTaskService;
        this.tslWaraHouseService = tslWaraHouseService;
    }

    /**
     * 手动入库
     * 
     * @param day
     * @return
     */
    @PostMapping("/warahouse")
    public R warahouseManually(@RequestBody String day) {
        try {
            tslWaraHouseService.dataWaraHouseTask(day);
            return R.ok().message("入库成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return R.error().message(String.format("文件缺失--%s", e.getMessage()));
        } catch (IOException e) {
            e.printStackTrace();
            return R.error().message(String.format("文件IO错误--%s", e.getMessage()));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message(String.format("发生错误--%s", e.getMessage()));
        }
    }

    /**
     * 手动入库HighQuality
     * 
     * @param day
     * @return
     */
    @PostMapping("/warahouse/highquality")
    public R warahouseHighQualityManually(@RequestBody String day) {
        try {
            tslWaraHouseService.waraHouseHighQuality(day);
            return R.ok().message("入库成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return R.error().message(String.format("文件缺失--%s", e.getMessage()));
        } catch (IOException e) {
            e.printStackTrace();
            return R.error().message(String.format("文件IO错误--%s", e.getMessage()));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message(String.format("发生错误--%s", e.getMessage()));
        }
    }

    /**
     * 手动入库MobileComp
     * 
     * @param day
     * @return
     */
    @PostMapping("/warahouse/mobilecomp")
    public R warahouseMobileCompManually(@RequestBody String day) {
        try {
            tslWaraHouseService.waraHouseMobileComp(day);
            return R.ok().message("入库成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return R.error().message(String.format("文件缺失--%s", e.getMessage()));
        } catch (IOException e) {
            e.printStackTrace();
            return R.error().message(String.format("文件IO错误--%s", e.getMessage()));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message(String.format("发生错误--%s", e.getMessage()));
        }
    }

    @PostMapping("/delete/mobilecomp")
    public R deleteMobileCompManually(@RequestBody String day) {
        try {
            tslWaraHouseService.deleteMobileCompForDay(day);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message(String.format("发生错误--%s", e.getMessage()));
        }
    }

    @PostMapping("/delete/highquality")
    public R deleteHighQualityManually(@RequestBody String day) {
        try {
            tslWaraHouseService.deleteHighQualityForDay(day);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message(String.format("发生错误--%s", e.getMessage()));
        }
    }

    /**
     * 检查数据源
     * 
     * @param day
     * @return
     */
    @PostMapping("/check/srcfile")
    public R checkScrFile(@RequestBody String day) {
        String check = tslWaraHouseService.check(day);
        if (check.endsWith("数据源正常。")) {
            return R.ok().message(check);
        } else {
            return R.error().message(check);
        }
    }

    /**
     * 手动生成报告
     * 
     * @param day
     * @return
     */
    @PostMapping("/report/generate")
    public R reportGenrateManully(@RequestBody String day) {
        try {
            taskService.reportGenerateTask(day);
            taskService.screenShotTask(day);
            return R.ok().message("报告生成成功");
        } catch (NullPointerException e) {
            e.printStackTrace();
            return R.error().message(String.format("数据不全, 请检查数据库"));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message(String.format("发生未知错误--%s", e.getMessage()));
        }
    }

    /**
     * 启动定时任务
     * 
     * @return
     */
    @PostMapping("/cron/start")
    public R startCronTask() {
        cronTaskService.startCronTask();
        return R.ok().message("任务启动成功");
    }

    /**
     * 停止定时任务
     * 
     * @return
     */
    @PostMapping("/cron/stop")
    public R stopCronTask() {
        cronTaskService.stopCronTask();
        return R.ok().message("任务已停止");
    }

    /**
     * 查询定时任务状态
     * 
     * @return
     */
    @PostMapping("/cron/status")
    public R getCronTaskStatus() {
        String cronExpression = cronTaskService.getCronExpression();
        boolean started = cronTaskService.isStarted();
        String status = started ? "启动中" : "已停止";
        return R.ok().message(String.format("任务状态：%s, 定时表达式：%s", status, cronExpression));
    }

    /**
     * 修改定时任务定时表达式
     * 
     * @return
     */
    @PostMapping("/cron/expression")
    public R updateCronTaskExpression(@RequestBody String cronExpression) {
        try {
            CronExpression.parse(cronExpression);
        } catch (IllegalArgumentException e) {
            return R.error().message(String.format("输入的表达式有误, 任务状态不变：%s", e.getMessage()));
        }
        String oldCronExpression = cronTaskService.getCronExpression();
        cronTaskService.updateCronExpression(cronExpression);
        return R.ok().message(String.format("定时表达式更新成功, 新表达式：%s, 原表达式：%s", cronExpression, oldCronExpression));
    }
}
