package com.liquidnet.client.admin.web.controller.zhengzai.kylin;

import com.github.pagehelper.PageInfo;
import com.liquidnet.client.admin.common.annotation.Log;
import com.liquidnet.client.admin.common.core.controller.BaseController;
import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.common.core.page.TableDataInfo;
import com.liquidnet.client.admin.common.enums.BusinessType;
import com.liquidnet.client.admin.zhengzai.kylin.service.impl.KylinArtistServiceImpl;
import com.liquidnet.service.kylin.dao.KylinArtistDao;
import com.liquidnet.service.kylin.dto.param.ArtistParam;
import com.liquidnet.service.kylin.dto.param.ArtistSearchParam;
import com.liquidnet.service.kylin.dto.vo.ArtistVo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 艺人管理 后端管理控制器
 * </p>
 *
 * @author liquidnet
 * @since 2026-02-04
 */
@Api(tags = "艺人管理")
@Controller
@RequestMapping("/kylin/artist")
public class KylinArtistController extends BaseController {

    private final String prefix = "zhengzai/kylin/artist";

    @Value("${liquidnet.client.admin.platformUrl}")
    private String platformUrl;

    @Autowired
    private KylinArtistServiceImpl kylinArtistService;

    @GetMapping("/create")
    @RequiresPermissions("kylin:artist:create")
    public String create(ModelMap mmap) {
        mmap.put("platformUrl", platformUrl);
        return prefix + "/create";
    }

    @GetMapping("/update/{artistId}")
    @RequiresPermissions("kylin:artist:update")
    public String update(@PathVariable("artistId") String artistId, ModelMap mmap) {
        ArtistVo result = kylinArtistService.detail(artistId);
        mmap.put("ArtistVo", result);
        mmap.put("platformUrl", platformUrl);
        return prefix + "/update";
    }

    @RequiresPermissions("kylin:artist:list")
    @GetMapping()
    public String artists() {
        return prefix + "/artists";
    }

    @RequiresPermissions("kylin:artist:detail")
    @GetMapping("/detail/{artistId}")
    public String detail(@PathVariable("artistId") String artistId, ModelMap mmap) {
        ArtistVo result = kylinArtistService.detail(artistId);
        mmap.put("ArtistVo", result);
        return prefix + "/detail";
    }

    @RequiresPermissions("kylin:artist:list")
    @PostMapping("list")
    @ResponseBody
    public TableDataInfo artistList(ArtistSearchParam param) {
        PageInfo<KylinArtistDao> result = kylinArtistService.artistList(param);
        return getDataTable(result.getList());
    }

    @Log(title = "创建艺人", businessType = BusinessType.INSERT)
    @RequiresPermissions("kylin:artist:create")
    @PostMapping("create")
    @ResponseBody
    public AjaxResult createSave(ArtistParam param, String albumImagesJson) {
        // 校验艺人名称是否已存在
        Boolean exists = kylinArtistService.checkArtistNameExists(param.getArtistName(), null);
        if (exists) {
            return error("艺人名称已存在，不可重复创建");
        }

        // 解析相册图片JSON数组
        if (albumImagesJson != null && !albumImagesJson.isEmpty() && !albumImagesJson.equals("[]")) {
            try {
                com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
                List<String> albumImages = mapper.readValue(albumImagesJson, 
                    mapper.getTypeFactory().constructCollectionType(List.class, String.class));
                param.setAlbumImages(albumImages);
            } catch (Exception e) {
                return error("相册图片数据格式错误");
            }
        }

        Boolean res = kylinArtistService.create(param);
        if (res) {
            return success();
        } else {
            return error("添加艺人失败");
        }
    }


    @Log(title = "修改艺人", businessType = BusinessType.UPDATE)
    @RequiresPermissions("kylin:artist:update")
    @PostMapping("update")
    @ResponseBody
    public AjaxResult updateSave(ArtistParam param, String albumImagesJson) {
        // 校验艺人名称是否已被其他艺人使用
        Boolean exists = kylinArtistService.checkArtistNameExists(param.getArtistName(), param.getArtistId());
        if (exists) {
            return error("艺人名称已被其他艺人使用");
        }

        // 解析相册图片JSON数组
        if (albumImagesJson != null && !albumImagesJson.isEmpty() && !albumImagesJson.equals("[]")) {
            try {
                com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
                List<String> albumImages = mapper.readValue(albumImagesJson, 
                    mapper.getTypeFactory().constructCollectionType(List.class, String.class));
                param.setAlbumImages(albumImages);
            } catch (Exception e) {
                return error("相册图片数据格式错误");
            }
        }

        Boolean res = kylinArtistService.update(param);
        if (res) {
            return success();
        } else {
            return error("修改艺人失败");
        }
    }


    @Log(title = "删除艺人", businessType = BusinessType.DELETE)
    @RequiresPermissions("kylin:artist:delete")
    @PostMapping("delete")
    @ResponseBody
    public AjaxResult delete(ArtistParam param) {
        List<String> artistIds = param.getIds();
        Boolean result = kylinArtistService.delete(artistIds);
        if (result) {
            return success("删除成功");
        } else {
            return error("删除失败");
        }
    }

    /**
     * 检查艺人名称是否存在
     */
    @PostMapping("checkName")
    @ResponseBody
    public AjaxResult checkArtistName(String artistName, String artistId) {
        Boolean exists = kylinArtistService.checkArtistNameExists(artistName, artistId);
        if (exists) {
            return error("艺人名称已存在");
        }
        return success();
    }
}
