package com.test.test.controller;

import java.util.List;

import com.test.common.utils.SeleniumUtils;
import com.test.test.domain.UiAutomation;
import com.test.test.domain.qo.UiAutomationDataQO;
import com.test.test.domain.qo.UiAutomationQO;
import com.test.test.service.IUiAutomationService;
import com.test.test.service.IUiSceneStepsService;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.test.common.annotation.Log;
import com.test.common.core.controller.BaseController;
import com.test.common.core.domain.AjaxResult;
import com.test.common.enums.BusinessType;

import com.test.common.core.page.TableDataInfo;

/**
 * ui自动化Controller
 * 
 * @author test
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/test/automation")
@Slf4j
public class UiAutomationController extends BaseController
{
    @Autowired
    private IUiAutomationService uiAutomationService;
    @Autowired
    private IUiSceneStepsService uiSceneStepsService;

    /**
     * 查询ui自动化列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:automation:list')")
    @GetMapping("/list")
    public TableDataInfo list(UiAutomationQO uiAutomation)
    {
        startPage();
        List<UiAutomation> list = uiAutomationService.selectUiAutomationList(uiAutomation);
        return getDataTable(list);
    }

    /**
     * 获取ui自动化详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:automation:query')")
    @GetMapping(value = "getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(uiAutomationService.selectUiAutomationById(id));
    }

    /**
     * 新增ui自动化
     */
//    @PreAuthorize("@ss.hasPermi('system:automation:add')")
    @Log(title = "ui自动化", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody UiAutomationDataQO uiAutomationDataQO)
    {
        try{
            log.info("新增成功！");
            return  success(uiAutomationService.insertUiAutomation(uiAutomationDataQO));
        }catch (Exception e){
            log.error("新增失败!",e);
            return error("新增失败!");
        }

    }

    /**
     * 修改ui自动化
     */
//    @PreAuthorize("@ss.hasPermi('system:automation:edit')")
    @Log(title = "ui自动化", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody UiAutomationDataQO uiAutomationDataQO)
    {
        try{
            log.info("修改成功！");
            return success(uiAutomationService.updateUiAutomation(uiAutomationDataQO));
        }catch (Exception e){
            log.error("修改失败!",e);
            return error("修改失败!");
        }
    }

    /**
     * 删除ui自动化
     */
//    @PreAuthorize("@ss.hasPermi('system:automation:remove')")
    @Log(title = "ui自动化", businessType = BusinessType.UPDATE)
	@PutMapping("delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(uiAutomationService.deleteUiAutomationByIds(ids));
    }


    /**
     * 执行场景步骤
     */
//    @PreAuthorize("@ss.hasPermi('system:automation:remove')")
    @GetMapping("/executeStep")
    public AjaxResult executeStep(@RequestParam Long id)
    {
        try{
            String triggerMode = "1";   //手动执行
            log.info("执行完成!");
            return success(uiSceneStepsService.executeStep(id,triggerMode));
        } catch (Exception e) {
            log.error("执行错误!",e);
            return error("执行错误!"+e.getMessage());
        }
    }
}
