package cn.gov.customs.demo.government.mock;

import cn.gov.customs.cacp.sdks.auth.AuthAdapter;
import cn.gov.customs.cacp.sdks.auth.CacpUser;
import cn.gov.customs.cacp.sdks.auth.h4a.pojo.H4AUser;
import cn.gov.customs.cacp.sdks.core.result.Result;
import cn.gov.customs.cacp.sdks.core.user.annotation.LogonUser;
import cn.gov.customs.cacp.sdks.core.user.pojo.CacpLogonUser;
import cn.gov.customs.cacp.sdks.frame.pojo.FrameUserInfo;
import cn.gov.customs.cacp.sdks.frame.pojo.ThemeInfo;
import cn.gov.customs.demo.government.constants.GovernmentResultCodeMessage;
import com.google.common.collect.Lists;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @description TODO
 * @author sunxuewen
 * @since 2024/10/29 上午10:10
 */
@RestController
@RequestMapping("/mock")
public class MockUserController {

  private final AuthAdapter adapter;

  public MockUserController(AuthAdapter adapter) {
    this.adapter = adapter;
  }

  @GetMapping("/get-frame-user")
  public Result<FrameUserInfo> getFrameUser(@LogonUser CacpLogonUser logonUser) {
    if (logonUser == null) {
      return Result.success(null);
    }
    CacpUser cacpUser = this.adapter.getUser(logonUser.getViewCode(), logonUser.getParentId(), logonUser.getUserId());
    if (Objects.isNull(cacpUser)) {
      return Result.fail(GovernmentResultCodeMessage.NO_PERMISSION, "用户为" + logonUser.getFullPathName());
    }

    FrameUserInfo frameUser = new FrameUserInfo();
    frameUser.setUserId(logonUser.getUserId());
    frameUser.setParentId(logonUser.getParentId());
    frameUser.setUserName(cacpUser.getOguName());
    frameUser.setFullPathName(cacpUser.getFullPathName());
    frameUser.setPersonId(cacpUser.getPersonId());
    frameUser.setCustomsCode(logonUser.getCustomsCode());
    frameUser.setStatus(cacpUser.getStatus());
    frameUser.setSortOrder(cacpUser.getSortOrder());
    frameUser.setEmail(cacpUser.getEmail());
    frameUser.setMobile(cacpUser.getMobile());

    H4AUser h4aUser = (H4AUser) cacpUser;
    frameUser.setRankCode(h4aUser.getRankCode());
    frameUser.setRankName(h4aUser.getRankName());
    frameUser.setSideline(h4aUser.isSideline());

    frameUser.setIpAddress(logonUser.getIpAddress());
    frameUser.setViewCode(logonUser.getViewCode());
    frameUser.setLogonType(logonUser.getLogonType());
    frameUser.setLogonTime(logonUser.getLogonTime());

//    frameUser.setThemeCode("DEFAULT");
    ThemeInfo themeInfo = new ThemeInfo();
    themeInfo.setThemeCode("DEFAULT");
//    frameUser.setTheme(themeInfo);
//    frameUser.setDelegates(Lists.newArrayList());

    return Result.success(frameUser);
  }

  @GetMapping("/set-mock-user")
  public Result<Boolean> setMockUser(@RequestParam String viewCode, @RequestParam String fullPathName) {
    CacpUser cacpUser = this.adapter.getUserByPath(viewCode, fullPathName);
    if (Objects.isNull(cacpUser)) {
      return Result.fail(GovernmentResultCodeMessage.NO_PERMISSION, "用户为" + fullPathName);
    }

    CacpLogonUser logonUser = new CacpLogonUser();
    logonUser.setUserId(cacpUser.getOguId());
    logonUser.setUserName(cacpUser.getOguName());
    logonUser.setParentId(cacpUser.getParentId());
    logonUser.setFullPathName(cacpUser.getFullPathName());
    logonUser.setPersonId(cacpUser.getPersonId());
    logonUser.setCustomsCode(cacpUser.getCustomsCode());
    logonUser.setStatus("1");
    logonUser.setIpAddress("mock");
    logonUser.setViewCode(viewCode);
    logonUser.setLogonType("mock");
    logonUser.setLogonTime(LocalDateTime.now());
    MockUserHelper.setMockUser(logonUser);

    return Result.success(true);
  }

  @GetMapping("/clear-mock-user")
  public Result<Boolean> clearMockUser() {
    MockUserHelper.setMockUser(null);
    return Result.success(true);
  }

}
