using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using detect.gui.Models;
using detect.gui.Models.Entities;

namespace detect.gui.Services.Detect;

public class DetectTaskProgressService : ServiceBase<DetectTaskProgressService, DetectTaskProgressEntity>
{
    public ApiResponse<DetectTaskProgressEntity?> ListById(long id)
    {
        var item = Find(x => x.Id == id);
        return new ApiResponse<DetectTaskProgressEntity?>(0, "success", item);
    }
    
    public ApiResponse<List<DetectTaskProgressEntity>?> ListAll()
    {
        var items = FindList(x => true, "Id", true)
            .ToList();
        return new ApiResponse<List<DetectTaskProgressEntity>?>(0, "success", items);
    }
    
    public ApiResponse<PagedResult<DetectTaskProgressEntity>> Search(long? taskId = -1, int pageNum = 1, int pageSize = 10)
    {
        Expression<Func<DetectTaskProgressEntity,bool>> filter = x => taskId == -1 || x.TaskId == taskId;
        var total = Count(filter);
        var items = FindList(filter, "CreateTime", false)
            .Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
        var pagedResult = new PagedResult<DetectTaskProgressEntity>(pageNum, pageSize, total, items);
        return new ApiResponse<PagedResult<DetectTaskProgressEntity>>(0, "success", pagedResult);
    }
    
    public ApiResponse<DetectTaskProgressEntity?> AddData(DetectTaskProgressEntity entity)
    {
        var data = Add(entity);
        return new ApiResponse<DetectTaskProgressEntity?>(0, "success", data);
    }
    
    public ApiResponse<DetectTaskProgressEntity?> UpdateData(DetectTaskProgressEntity entity)
    {
        return new ApiResponse<DetectTaskProgressEntity?>(Update(entity) ? 0 : -1, "success", null);
    }
    
    public ApiResponse<DetectTaskProgressEntity?> DeleteById(long id)
    {
        Delete(x => x.Id == id);
        return new ApiResponse<DetectTaskProgressEntity?>(0, "success", null);
    }
    
    public ApiResponse<DetectTaskProgressEntity?> DeleteData(DetectTaskProgressEntity entity)
    {
        Delete(entity);
        return new ApiResponse<DetectTaskProgressEntity?>(0, "success", null);
    }
}