package com.zcxk.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.zcxk.model.LoginUser;
import com.zcxk.model.ProgramItem;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import java.util.LinkedHashMap;
import java.util.List;

public class UserUtil {
    /**
     *获取当前用户
     */
    public static LoginUser getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            if (authentication instanceof AnonymousAuthenticationToken) {
                return new LoginUser(null);
            }

            if (authentication instanceof UsernamePasswordAuthenticationToken) {
                return (LoginUser) authentication.getPrincipal();
            }

            if(authentication instanceof OAuth2Authentication){
                OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
                if(oAuth2Authentication.getPrincipal() instanceof LoginUser){
                    return (LoginUser) oAuth2Authentication.getPrincipal();
                }else {
                    LinkedHashMap<String,Object> details = (LinkedHashMap<String,Object>) oAuth2Authentication.getUserAuthentication().getDetails();
                    LinkedHashMap<String,Object> principalMap = (LinkedHashMap<String, Object>) details.get("principal");

                    JSONObject userJson = (JSONObject) JSONObject.toJSON(principalMap);

                    LoginUser loginUser = new LoginUser(null);
                    loginUser.setId((Integer) userJson.get("id"));
                    loginUser.setUsername((String) userJson.get("username"));
                    //loginUser.setName((String) userJson.get("name"));
                    loginUser.setPhoneNumber((String) userJson.get("phoneNumber"));
                    loginUser.setType((String) userJson.get("type"));
                    //loginUser.setName(user.getUsername());
                    loginUser.setCompanyId(userJson.getInteger("companyId"));
                    loginUser.setDepartmentId(userJson.getInteger("departmentId"));
                    loginUser.setTenantId(userJson.getString("tenantId"));
                    JSONArray programItemListArray = userJson.getJSONArray("programItemList");
                    loginUser.setPermissonType(userJson.getInteger("permissonType"));
                    JSONArray appIds = userJson.getJSONArray("appIds");
                    if (programItemListArray != null) {
                        List<ProgramItem> collection =
                                JSONObject.parseArray(programItemListArray.toJSONString(), ProgramItem.class);
                        loginUser.setProgramItemList(collection);
                    }
                    if(appIds!=null){
                        List<String> appIdString = JSONObject.parseArray(appIds.toJSONString(), String.class);
                        loginUser.setAppIds(appIdString);
                    }
                    return loginUser;
                }
            }
        }

        return new LoginUser(null);
    }
}
