package com.seeyon.apps.bip.ctrl;

import cn.hutool.http.HttpRequest;
import cn.hutool.log.Log;
import com.seeyon.apps.addressbook.manager.AddressBookCustomerFieldInfoManager;
import com.seeyon.apps.addressbook.po.AddressBook;
import com.seeyon.apps.bip.constants.BipConstants;
import com.seeyon.apps.bip.util.BipHttpUtil;
import com.seeyon.apps.bip.vo.BipResponse;
import com.seeyon.apps.common.config.ICstConfigApi;
import com.seeyon.apps.common.plugin.vo.ConfigVo;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.util.Strings;
import org.springframework.web.servlet.ModelAndView;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 功能描述:<br>
 * <pre>
 *
 * </pre>
 *
 * @Author: FanGaowei
 * @Date: 2022/12/1
 */
public class NccWageSsoController extends BaseController {

    private static Log log = Log.get(NccWageSsoController.class);
    @Inject
    private ICstConfigApi cstConfigApi;
    @Inject
    private AddressBookCustomerFieldInfoManager addressBookCustomerFieldInfoManager;

    @Override
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
        StringBuffer localUrl = request.getRequestURL();
        String v5ticket = request.getParameter("ticket");
        ConfigVo config = cstConfigApi.getConfig(BipConstants.getPluginId());
        String url = config.getParamVal(BipConstants.ssoUrl.name());
        AddressBook addressBook = addressBookCustomerFieldInfoManager.getByMemberId(AppContext.currentUserId());
        if(null == addressBook) {
            super.rendJavaScript(response, "alert('BIP账号未维护，无法单点登录！');");
            return null;
        }
        String code = addressBook.getExtAttr2();
        if(Strings.isBlank(code)) {
            super.rendJavaScript(response, "alert('BIP账号未维护，无法单点登录！');");
            return null;
        }
        // 获取token走内网地址
        String interUrl = config.getParamVal(BipConstants.bipUrl.name());
        String tokenUrl = interUrl + "/servlet/NCTokenServlet?dsname=" + config.getParamVal(BipConstants.dsname.name())
                + "&usercode=" + code + "&system=OA&busicentercode=" + config.getParamVal(BipConstants.busicentercode.name());
        try {
            String body = HttpRequest.get(tokenUrl).execute().body();
            BipResponse bipResponse = BipHttpUtil.parseResponse(body);
            if(bipResponse.isSuccess()) {
//                String ssoUrl = url + "/nccloud/resources/uap/rbac/thirdpartylogin/main/index.html?accesstoken=" + bipResponse.getData()
//                        + "&redirect_uri=" + url + "/servlet/ApproveTodo?usercode="+ code +"&salary=60656013p&v5ticket="+v5ticket;
                String ssoUrl = url +config.getParamVal(BipConstants.bipWageUrl.name())+v5ticket;
//                String ssoUrl = url + "/nccloud/resources/uap/rbac/thirdpartylogin/main/index.html?accesstoken=" + bipResponse.getData()
//                        + "&redirect_uri=" + url + config.getParamVal(BipConstants.bipWageUrl.name())+v5ticket;
                ssoUrl = ssoUrl.replace("${code}",code);
                System.out.println(ssoUrl);
                response.sendRedirect(ssoUrl);
            } else {
                super.rendJavaScript(response, "alert('单点登录错误：" + bipResponse.getMsg() + "');");
            }
        } catch (Exception e) {
            logger.error("单点登录异常", e);
            super.rendJavaScript(response, "alert('后台发生错误');");
        }
        return null;
    }
}
