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 DetectTaskService : ServiceBase<DetectTaskService, DetectTaskEntity>
{
    public ApiResponse<DetectTaskEntity?> ListById(long id)
    {
        var item = Find(x => x.Id == id);
        return new ApiResponse<DetectTaskEntity?>(0, "success", item);
    }
    
    public ApiResponse<List<DetectTaskEntity>?> ListAll()
    {
        var items = FindList(x => true, "Id", true)
            .ToList();
        return new ApiResponse<List<DetectTaskEntity>?>(0, "success", items);
    }
    
    public ApiResponse<List<DetectTaskEntity>?> ListByIds(long[] ids)
    {
        var items = FindList(x => ids.Contains(x.Id!.Value), "Id", true)
            .ToList();
        return new ApiResponse<List<DetectTaskEntity>?>(0, "success", items);
    }
    
    public ApiResponse<PagedResult<DetectTaskEntity>> Search(string? name = "", int pageNum = 1, int pageSize = 10)
    {
        Expression<Func<DetectTaskEntity,bool>> filter = x => (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(x.Name) || x.Name.Contains(name));
        var total = Count(filter);
        var items = FindList(filter, "CreateTime", false)
            .Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
        var pagedResult = new PagedResult<DetectTaskEntity>(pageNum, pageSize, total, items);
        return new ApiResponse<PagedResult<DetectTaskEntity>>(0, "success", pagedResult);
    }
    
    public ApiResponse<DetectTaskEntity?> AddData(DetectTaskEntity entity)
    {
        var data = Add(entity);
        return new ApiResponse<DetectTaskEntity?>(0, "success", data);
    }
    
    public ApiResponse<DetectTaskEntity?> UpdateData(DetectTaskEntity entity)
    {
        return new ApiResponse<DetectTaskEntity?>(Update(entity) ? 0 : -1, "success", null);
    }
    
    public ApiResponse<DetectTaskEntity?> DeleteById(long id)
    {
        Delete(x => x.Id == id);
        return new ApiResponse<DetectTaskEntity?>(0, "success", null);
    }
    
    public ApiResponse<DetectTaskEntity?> DeleteData(DetectTaskEntity entity)
    {
        Delete(entity);
        return new ApiResponse<DetectTaskEntity?>(0, "success", null);
    }
}