package com.heyu.api.controller.mm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.resource.*;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvResourceDao;
import com.heyu.api.data.dto.vv.VVResourceDTO;
import com.heyu.api.data.entity.vv.VvResourceEntity;
import com.heyu.api.data.utils.R;
import com.heyu.api.utils.ISelect;
import com.heyu.api.utils.PPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/mm/resource")
public class AdminResourceController {


    @Autowired
    private VvResourceDao vvResourceDao;


    /***
     *  https://api.1024api.com/api-interface//mm/resource/list
     *
     *  http://localhost:8888/mm/resource/list
     */
    @Describe("资源列表")
    @RequestMapping("/list")
    public R list(@RequestBody VvAdminResourceRequest adminResourceRequest) {

        PPageUtils pageUtils = PPageUtils.startPage(adminResourceRequest.getPageNum(), adminResourceRequest.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvResourceDao.selectByCondition(page,
                                adminResourceRequest.getFileName(),
                                adminResourceRequest.getType(),
                                adminResourceRequest.getParentId(),
                                adminResourceRequest.getMinCreateTimestamp(),
                                adminResourceRequest.getMaxCreateTimestamp(),
                                adminResourceRequest.getMinModifyTimestamp(),
                                adminResourceRequest.getMaxModifyTimestamp(),
                                adminResourceRequest.getCreateTimestampSort(),
                                adminResourceRequest.getModifyTimestampSort()

                        );
                    }
                });
        List<VvResourceListDTO> vvResourceDTOS = new ArrayList<>();
        List<VvResourceEntity> vvResourceEntities = pageUtils.getRows();

        List<VvResourceEntity> vvResourceEntityList = new ArrayList<>();
        getParentResource(adminResourceRequest.getParentId(), vvResourceEntityList);
        for (VvResourceEntity vvResourceEntity : vvResourceEntities) {
            VvResourceListDTO vvResourceDTO = new VvResourceListDTO();
            BeanUtils.copyProperties(vvResourceEntity, vvResourceDTO);


            if(adminResourceRequest.getParentId() ==null){
                List<VvResourceEntity> vvResourceEntityListTemp = new ArrayList<>();
                getParentResource(vvResourceEntity.getParentId(), vvResourceEntityListTemp);
                vvResourceDTO.setLocations(vvResourceEntityListTemp);
            }else{
                vvResourceDTO.setLocations(vvResourceEntityList);
            }


            vvResourceDTOS.add(vvResourceDTO);
        }
        pageUtils.setRows(vvResourceDTOS);

        return R.ok().setData(pageUtils);
    }

    /***
     * @param parentId
     */
    public void getParentResource(Long parentId, List<VvResourceEntity> vvResourceEntityList) {
        if (parentId ==null || parentId <= 0) {
            return;
        }

        VvResourceEntity vvResource = vvResourceDao.selectVvResourceById(parentId);
        if(vvResource == null){
            return ;
        }
        vvResourceEntityList.add(0,vvResource);
        getParentResource(vvResource.getParentId(), vvResourceEntityList);
    }

    /***
     * test2::test1
     *
     * 资源新增或修改
     *
     *
     * 这里包含移动
     *
     *
     * /mm/resource/addOrUpdate
     */
    @Describe("资源添加")
    @RequestMapping("/addOrUpdate")
    public R addOrUpdate(@RequestBody List<VvResourceEntity> resourceAddOrUpdateRequests) {
        for (VvResourceEntity resourceAddOrUpdateRequest : resourceAddOrUpdateRequests) {
            if (resourceAddOrUpdateRequest.getId() == null) {
                resourceAddOrUpdateRequest.setCreateTimestamp(System.currentTimeMillis());
            }
            resourceAddOrUpdateRequest.setModifyTimestamp(System.currentTimeMillis());
            vvResourceDao.insertOrUpdateVvResource(resourceAddOrUpdateRequest);
        }
        return R.ok("保存成功");
    }

    /***
     * 资源排序
     * @param vvAdminResourceSortRequest
     * @return
     *
     * /mm/resource/sort
     */
    @Describe("资源排序")
    @RequestMapping("/sort")
    public R sort(@RequestBody VvAdminResourceSortRequest vvAdminResourceSortRequest) {
        List<VvResourceEntity> vvResourceEntities = vvResourceDao.selectVvResourceByIds(vvAdminResourceSortRequest.getResourceIds());
        for (VvResourceEntity vvResourceEntity : vvResourceEntities) {
            vvResourceEntity.setDefaultSort(vvAdminResourceSortRequest.getResourceIds().indexOf(vvResourceEntity.getId()) + 1);
            vvResourceDao.updateCoverVvResourceById(vvResourceEntity);
        }
        return R.ok();
    }


    /***
     * 资源删除
     * /mm/resource/delete
     */
    @Describe("资源删除")
    @RequestMapping("/delete")
    public R delete(@RequestBody VvAdminResourceDeleteRequest adminResourceDeleteRequest) {
        for (Long resourceId : adminResourceDeleteRequest.getResourceIds()) {


            doBatchDelete(resourceId);

        }
        return R.ok("删除成功");
    }

    public void doBatchDelete(Long resourceId) {
        VVResourceDTO vvResourceEntity = vvResourceDao.selectDTOById(resourceId);
        getChildResourceList(vvResourceEntity);
        doDeleteChild(vvResourceEntity);
    }


    public void doDeleteChild(VVResourceDTO vvResourceDTO) {
        vvResourceDao.deleteVvResourceById(vvResourceDTO.getId());

        if (CollectionUtils.isEmpty(vvResourceDTO.getChildList())) {
            return;
        }

        for (VVResourceDTO vvResourceEntity : vvResourceDTO.getChildList()) {
            doDeleteChild(vvResourceEntity);
        }
    }

    /***
     * 资源复制
     *
     */
    @Describe("资源复制")
    @RequestMapping("/copy")
    public R copy(@RequestBody VvAdminResourceCopyRequest vvAdminResourceCopyRequest) {


        for (Long resourceId : vvAdminResourceCopyRequest.getResourceIds()) {

            doCopy(resourceId, vvAdminResourceCopyRequest.getParentId());
        }

        return R.ok("复制成功");
    }

    @Describe("资源树")
    @RequestMapping("/tree")
    public R tree(@RequestBody ResourceTreeDTO resourceTreeDTO) {


        Long resourceId = resourceTreeDTO.getResourceId();

        VVResourceDTO vvResourceEntity = vvResourceDao.selectDTOById(resourceId);

        getChildResourceList(vvResourceEntity);


        return R.ok().setData(vvResourceEntity);

    }


    public void doCopy(Long resourceId, Long parentId) {
        VVResourceDTO vvResourceEntity = vvResourceDao.selectDTOById(resourceId);
        vvResourceEntity.setParentId(parentId);

        getChildResourceList(vvResourceEntity);

        doAddChild(vvResourceEntity);

    }

    public void doAddChild(VVResourceDTO vvResourceDTO) {
        vvResourceDTO.setId(null);
        vvResourceDao.insertVvResource(vvResourceDTO);

        if (CollectionUtils.isEmpty(vvResourceDTO.getChildList())) {

            return;
        }

        for (VVResourceDTO vvResourceEntity : vvResourceDTO.getChildList()) {
            vvResourceEntity.setParentId(vvResourceDTO.getId());
            doAddChild(vvResourceEntity);
        }
    }


    public void getChildResourceList(VVResourceDTO vvResourceDTO) {

        List<VVResourceDTO> vvResourceEntities = vvResourceDao.selectDtoByParentId(vvResourceDTO.getId());

        if (CollectionUtils.isEmpty(vvResourceEntities)) {

            return;
        }

        vvResourceDTO.setChildList(vvResourceEntities);

        for (VVResourceDTO vvResourceEntity : vvResourceEntities) {

            getChildResourceList(vvResourceEntity);
        }

    }

}
