package com.jty.wsxt.interfaces;

import com.jty.wsxt.domain.model.auth.authority.AuthAuthority;
import com.jty.wsxt.domain.model.auth.role.AuthRole;
import com.jty.wsxt.domain.model.auth.user.nrsc.NrscTeacher;
import com.jty.wsxt.interfaces.dto.AuthAuthorityDto;
import com.jty.wsxt.interfaces.dto.AuthRoleDto;
import com.jty.wsxt.interfaces.dto.NrscRoleDto;
import com.jty.wsxt.interfaces.dto.NrscTeacherDto;
import org.apache.commons.collections.CollectionUtils;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;

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

/**
 * Assembler
 *
 * @author Manjiajie
 * @since 2019-6-13 14:03:45
 */
public class Assembler {
    public static AuthRoleDto cashToAuthRoleDto(AuthRole role){
        AuthRoleDto roleDto = new AuthRoleDto();
        List<Integer> authAuthorityIds = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(role.getAuthorities())){
            role.getAuthorities().forEach(authority->authAuthorityIds.add(authority.getId()));
        }
        roleDto.setAuthAuthoritiesIds(authAuthorityIds);
        roleDto.setName(role.getName());
        roleDto.setId(role.getId());
        return roleDto;
    }

    public static NrscTeacherDto castToNrscTeacherDto(NrscTeacher nrscTeacher){
        NrscTeacherDto dto=new NrscTeacherDto();
        dto.setId(nrscTeacher.getId());
        dto.setAccount(nrscTeacher.getAuthUser().getAccount());
        dto.setUsername(nrscTeacher.getAuthUser().getRealName());
        dto.setLastLoginTime(nrscTeacher.getLastLoginTime());
        dto.setEnabled(nrscTeacher.getAuthUser().getEnabled());
        dto.setStageName(nrscTeacher.getStage().getName());
        dto.setStageId(nrscTeacher.getStage().getId());
        dto.setSubjectName(nrscTeacher.getSubject().getName());
        dto.setSubjectId(nrscTeacher.getSubject().getId());
        if(CollectionUtils.isNotEmpty(nrscTeacher.getAuthUser().getRoles())){
            ModelMapper modelMapper=new ModelMapper();
            List<AuthRole> roleList = new ArrayList<>();
            nrscTeacher.getAuthUser().getRoles().forEach(authRole -> {
                if("nrsc".equals(authRole.getPlatform())){
                    roleList.add(authRole);
                }
            });
            roleList.forEach(role -> {

            });
            List<NrscRoleDto> list=modelMapper.map(roleList,new TypeToken<List<NrscRoleDto>>(){}.getType());
            dto.setRoles(list);
        }
        return dto;
    }

    public static AuthAuthorityDto castToAuthAuthorityDto(AuthAuthority authAuthority){
        AuthAuthorityDto authorityDto = new AuthAuthorityDto();
        authorityDto.setId(authAuthority.getId());
        authorityDto.setCode(authAuthority.getCode());
        authorityDto.setDepth(authAuthority.getDepth());
        authorityDto.setName(authAuthority.getName());
        authorityDto.setOrdinal(authAuthority.getOrdinal());
        authorityDto.setPathIds(authAuthority.getPathIds());
        authorityDto.setUrl(authAuthority.getUrl());
        List<AuthAuthorityDto> childList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(authAuthority.getChildren())){
            authAuthority.getChildren().forEach(child->childList.add(Assembler.castToAuthAuthorityDto(child)));
        }
        authorityDto.setChildren(childList);
        return authorityDto;
    }


}