package jnpf.tempmodule.controller;

import jnpf.base.ActionResult;
import jnpf.model.tempmodule.AppBannerVo;
import jnpf.model.tempmodule.RollImageVo;
import jnpf.model.tempmodule.dto.RollImageDto;
import jnpf.tempmodule.service.AppBannerService;
import jnpf.tempmodule.service.RollImageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 临时功能控制器
 *
 * @author yanwenfu
 * @create 2023-10-10
 */
@RestController
@RequestMapping(value = "/tempmodule")
public class TempModuleController {

    @Resource
    private RollImageService rollImageService;

    @Resource
    private AppBannerService appBannerService;

    @GetMapping(value = "/banner/list")
    public ActionResult<List<AppBannerVo>> getAppBannerList(@RequestHeader(value = "User-Agent", required = false) String userAgent) {

        if (StringUtils.isNotEmpty(userAgent)) {
            if (userAgent.equals("1 android") || userAgent.equals("1 iphone")) {
                List<RollImageVo> list = rollImageService.getRollImageList(0);
                if (list.isEmpty()) {
                    return ActionResult.success(new ArrayList<>());
                }
                List<AppBannerVo> bannerList = new ArrayList<>();
                list.forEach(rollImage -> {
                    AppBannerVo bannerVo = new AppBannerVo(rollImage.getImageUrl(), rollImage.getJumpTo(), rollImage.getSort());
                    bannerList.add(bannerVo);
                });
                return ActionResult.success(bannerList);
            }
        }
        List<AppBannerVo> list = appBannerService.getAppBannerList();
        return ActionResult.success(list);
    }

    /**
     * pc端获取banner列表
     * @return {@link ActionResult }<{@link List }<{@link AppBannerVo }>>
     */
    @GetMapping(value = "/banner/list-pc")
    public ActionResult<List<AppBannerVo>> getAppBannerListPc() {
        List<RollImageVo> list = rollImageService.getRollImageList(1);
        if (list.isEmpty()) {
            return ActionResult.success(new ArrayList<>());
        }
        List<AppBannerVo> bannerList = new ArrayList<>();
        list.forEach(rollImage -> {
            AppBannerVo bannerVo = new AppBannerVo(rollImage.getImageUrl(), rollImage.getJumpTo(), rollImage.getSort());
            bannerList.add(bannerVo);
        });
        return ActionResult.success(bannerList);
    }

    /**
     * 获取banner列表
     * @param bannerType banner类型 0app1pc
     * @return {@link ActionResult }<{@link List }<{@link RollImageVo }>>
     */
    @GetMapping(value = "/rollImage/list")
    public ActionResult<List<RollImageVo>> getRollImageList(Integer bannerType) {
        List<RollImageVo> list = rollImageService.getRollImageList(bannerType);
        return ActionResult.success(list);
    }

    /**
     * 新增轮播图
     * @param rollImageDto 滚动图dto
     * @return jnpf.base.ActionResult<java.lang.Object>
     */
    @PostMapping(value = "/rollImage")
    public ActionResult<Object> addRollImage(@RequestBody @Valid RollImageDto rollImageDto) {
        rollImageService.addRollImage(rollImageDto);
        return ActionResult.success();
    }

    /**
     * 删除轮播图
     * @param id 轮播图id
     * @return jnpf.base.ActionResult<java.lang.Object>
     */
    @DeleteMapping(value = "/rollImage/{id}")
    public ActionResult<Object> deleteRollImage(@PathVariable(value = "id") String id) {

        rollImageService.deleteRollImage(id);
        return ActionResult.success();
    }
}
