package com.nokia.controller;

import com.nokia.common.R;
import com.nokia.pojo.User;
import com.nokia.service.AclService;
import com.nokia.vo.DapingUserVo;
import com.nokia.vo.FenxiUserVo;
import com.nokia.vo.LiuchengUserVo;
import com.nokia.vo.TokenVo;
import com.nokia.vo.YuceUserVo;
import com.nokia.vo.HeidiankuUserVo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api")
public class VerificationController {

    @Autowired
    private AclService aclService;

    @Value("${dop.redirect.url}")
    private String dopRedirectUrl;
    @Value("${top.redirect.url}")
    private String topRedirectUrl;

    @PostMapping("/verification")
    public R verifySystem(@RequestBody TokenVo tokenEntity) {
        log.debug("输入参数: {}", tokenEntity);
        User userEntity = aclService.verifySystem(tokenEntity.getToken(), tokenEntity.getFromSystem());
        log.debug("输出用户信息: {}", userEntity);
        Map<String, String> map = new HashMap<>();
        if (tokenEntity.getFromSystem().equalsIgnoreCase("top")) {
            map.put("redirect", topRedirectUrl);
        } else if (tokenEntity.getFromSystem().equalsIgnoreCase("dop")) {
            map.put("redirect", dopRedirectUrl);
        } else {
            switch (tokenEntity.getSystem().trim().toLowerCase()) {
                case "daping":
                    map.put("redirect", dopRedirectUrl);
                    break;
                case "liucheng":
                case "fenxi":
                case "yuce":
                case "heidianku":
                default:
                    map.put("redirect", topRedirectUrl);
                    break;
            }
        }
        if (userEntity == null) {
            return R.error().data(map).message("用户不存在");
        } else {
            switch (tokenEntity.getSystem().trim().toLowerCase()) {
                case "liucheng":
                    return R.ok().data(new LiuchengUserVo(userEntity));
                case "fenxi":
                    FenxiUserVo fenxiUserVo = new FenxiUserVo(userEntity);
                    if (fenxiUserVo.getCity() == null) {
                        return R.error().data(map).message("用户不存在");
                    }
                    return R.ok().data(fenxiUserVo);
                case "daping":
                    DapingUserVo dapingUserVo = new DapingUserVo(userEntity);
                    if (dapingUserVo.getCity() == null) {
                        return R.error().data(map).message("用户不存在");
                    }
                    return R.ok().data(dapingUserVo);
                case "yuce":
                    YuceUserVo yuceUserVo = new YuceUserVo(userEntity);
                    if (yuceUserVo.getCity() == null) {
                        return R.error().data(map).message("用户不存在");
                    }
                    return R.ok().data(yuceUserVo);
                case "heidianku":
                    HeidiankuUserVo heidiankuUserVo = new HeidiankuUserVo(userEntity);
                    if (heidiankuUserVo.getCity() == null) {
                        return R.error().data(map).message("用户不存在");
                    }
                    return R.ok().data(heidiankuUserVo);
                default:
                    return R.error().message("当前允许的system为: liucheng/fenxi/daping/yuce/heidianku");
            }
        }
    }
}
