package com.izouma.nineth.web;

import com.izouma.nineth.domain.MetaPlayerWear;
import com.izouma.nineth.domain.MetaPlayerWearCount;
import com.izouma.nineth.dto.MetaRestResult;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.enums.MetaPlayerWearEnum;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.MetaPlayerWearRepo;
import com.izouma.nineth.service.MetaPlayerWearService;
import com.izouma.nineth.utils.ObjUtils;
import com.izouma.nineth.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/metaPlayerWear")
@AllArgsConstructor
public class MetaPlayerWearController extends BaseController {
    private MetaPlayerWearService metaPlayerWearService;
    private MetaPlayerWearRepo metaPlayerWearRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public MetaPlayerWear save(@RequestBody MetaPlayerWear record) {
        if (record.getId() != null) {
            MetaPlayerWear orig = metaPlayerWearRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return metaPlayerWearRepo.save(orig);
        }
        return metaPlayerWearRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<MetaPlayerWear> all(@RequestBody PageQuery pageQuery) {
        return metaPlayerWearService.all(pageQuery);
    }

    @GetMapping("/get/{id}")
    public MetaPlayerWear get(@PathVariable Long id) {
        return metaPlayerWearRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        metaPlayerWearRepo.softDelete(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<MetaPlayerWear> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @GetMapping("/{userId}/allWears")
    public MetaRestResult<MetaPlayerWearCount> allWears(@PathVariable Long userId) {
        MetaPlayerWearCount metaPlayerWearCount = new MetaPlayerWearCount();
        metaPlayerWearCount.setUserId(userId);
        try {
            Arrays.stream(MetaPlayerWearEnum.values()).forEach(metaPlayerWearEnum -> {
                List<MetaPlayerWear> metaPlayerWears = metaPlayerWearRepo.findAllByUserIdAndType(userId, metaPlayerWearEnum);
                switch (metaPlayerWearEnum) {
                    case HEADDRESS:
                        metaPlayerWearCount.setHeaddressList(metaPlayerWears);
                        break;
                    case CLOTHES:
                        metaPlayerWearCount.setClothesList(metaPlayerWears);
                        break;
                    case TROUSERS:
                        metaPlayerWearCount.setTrousersList(metaPlayerWears);
                        break;
                    case SHOES:
                        metaPlayerWearCount.setShoesList(metaPlayerWears);
                        break;
                    case ACTION:
                        metaPlayerWearCount.setActionList(metaPlayerWears);
                        break;
                    default:
                        throw new BusinessException("不支持的服饰类型");
                }
            });
        } catch (Exception e) {
            return MetaRestResult.returnError(e.getMessage());
        }
        return MetaRestResult.returnSuccess(metaPlayerWearCount);
    }
}

