package com.bcxin.backend.domain.system.dispatch;


import com.bcxin.backend.domain.system.entities.DepartmentDTO;
import com.bcxin.backend.domain.system.entities.UserDTO;
import com.bcxin.backend.domain.system.configs.DispatchSiteResourceProperties;
import com.bcxin.backend.domain.system.dispatch.snapshots.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public final class DispatchTranslateUtil {

    public static UserValueSnapshot translate(UserDTO userVO, DispatchSiteResourceProperties resourceProperties) {
        String defaultDepartId = userVO.getDefaultDepartment();

        Collection<String> selectedDepartIds = new ArrayList<>();
        if (userVO.getDepartments() != null) {
            for (DepartmentDTO departmentVO : userVO.getDepartments()) {
                selectedDepartIds.add(departmentVO.getId());
            }
        }

        String resourcePrefix = resourceProperties.getPrefix();


        UserValueSnapshot valueSnapshot = UserValueSnapshot.create(
                userVO.getId(), userVO.getName(),
                userVO.getLoginno(),
                userVO.getLoginpwd(),
                userVO.getTelephone(),
                userVO.getEmail(),
                userVO.getField11(),
                userVO.getField12(),
                userVO.getField13(),
                userVO.getField20(),
                defaultDepartId,
                userVO.getDomainid(),
                userVO.getField20(),
                userVO.getField18(),
                "",
                "",
                userVO.getLevel(),
                selectedDepartIds,
                resourcePrefix,
                userVO.getLastModifyTime(),
                getExtendMap(userVO)
                );
        valueSnapshot.setLevel(userVO.getLevel());
        valueSnapshot.setLiaisonOfficer(userVO.isLiaisonOfficer());
        valueSnapshot.setDomainUser(userVO.getDomainUser());

        /**
         * 离职为0, 否则为1
         */
        valueSnapshot.setIsDimission(userVO.getDimission() == 0);

        return valueSnapshot;
    }

    private static Map<String,Object> getExtendMap(UserDTO userVO) {
        Map<String, Object> map = new HashMap<>();
        map.put("Field1", userVO.getField1());
        map.put("Field2", userVO.getField2());
        map.put("Field3", userVO.getField3());
        map.put("Field4", userVO.getField4());
        map.put("Field5", userVO.getField5());
        map.put("Field6", userVO.getField6());
        map.put("Field7", userVO.getField7());
        map.put("Field8", userVO.getField8());
        map.put("Field9", userVO.getField9());
        map.put("Field10", userVO.getField10());
        map.put("Field11", userVO.getField11());
        map.put("Field12", userVO.getField12());
        map.put("Field13", userVO.getField13());
        map.put("Field14", userVO.getField14());
        map.put("Field15", userVO.getField15());
        map.put("Field16", userVO.getField16());
        map.put("Field17", userVO.getField17());
        map.put("Field18", userVO.getField18());
        map.put("Field19", userVO.getField19());
        map.put("Field20", userVO.getField20());
        map.put("Field21", userVO.getField21());
        map.put("Field22", userVO.getField22());
        map.put("Field23", userVO.getField23());
        map.put("Field24", userVO.getField24());
        map.put("Field25", userVO.getField25());

        return map;
    }
}
