package com.fs.app.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.fs.app.annotation.Login;
import com.fs.app.annotation.UserOperationLog;
import com.fs.app.utils.JwtUtils;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.enums.ImTypeEnum;
import com.fs.common.param.LoginMaWxParam;
import com.fs.common.param.LoginParam;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.IpUtil;
import com.fs.company.domain.CompanyUser;
import com.fs.core.config.WxMaConfiguration;
import com.fs.core.config.WxMpConfiguration;
import com.fs.course.config.CourseMaConfig;
import com.fs.his.config.FsSysConfig;
import com.fs.his.domain.*;
import com.fs.his.enums.FsUserOperationEnum;
import com.fs.his.mapper.FsUserLoginLogMapper;
import com.fs.his.service.IFsUserService;
import com.fs.his.service.IFsUserWxService;
import com.fs.his.utils.ConfigUtil;
import com.fs.im.config.ImTypeConfig;
import com.fs.im.service.OpenIMService;
import com.fs.system.domain.SysConfig;
import com.fs.system.mapper.SysConfigMapper;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import static com.fs.his.utils.PhoneUtil.encryptPhone;

/**
 * 微信小程序用户接口
 *
 */
@Api("微信接口")
@RestController
@RequestMapping(value="/app/wx")
public class WxUserController extends AppBaseController{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ConfigUtil configUtil;
    @Autowired
    private IFsUserService userService;
    @Autowired
    FsUserLoginLogMapper fsUserLoginLogMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    RedisCache redisCache;

    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private IFsUserWxService userWxService;

    @Autowired
    private OpenIMService openIMService;


    /**
     * 登陆接口
     */
    /**
     * 登陆接口
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    @Transactional
    @UserOperationLog(operationType = FsUserOperationEnum.MINLOGIN)
    public R login( @RequestBody LoginParam param) {
        if (StringUtils.isBlank(param.getCode())) {
            return R.error("code不存在");
        }
        String appId = param.getAppId();
        if (StringUtils.isBlank(appId)){
            FsSysConfig con = configUtil.getSysConfig();
            appId = con.getAppid();
        }

        final WxMaService wxService = WxMaConfiguration.getMaService(appId);
        try {
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(param.getCode());
            this.logger.info(session.getSessionKey());
            this.logger.info(session.getOpenid());
            // 解密
            WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(session.getSessionKey(), param.getEncryptedData(), param.getIv());
            //三端用户同步，先用unionid查询
            FsUser user = new FsUser();
            if (session.getUnionid()!=null){
                user = userService.selectFsUserByUnionid(session.getUnionid());
                if (user==null){
                    user = userService.selectFsUserByOpenId(session.getOpenid());
                }
            }else {
                user = userService.selectFsUserByOpenId(session.getOpenid());
            }

            if(user==null){
                //新用户
                String phoneNumber = phoneNoInfo.getPhoneNumber();
                //查询手机号是否存在，如果存在，更新
                FsUser checkPhone=userService.selectFsUserByPhone(encryptPhone(phoneNumber));
                if (checkPhone==null){
                    checkPhone=userService.selectFsUserByPhone(phoneNumber);
                }
                if(checkPhone!=null){
                    user=checkPhone;
                    FsUser userMap=new FsUser();
                    userMap.setMaOpenId(session.getOpenid());
                    userMap.setUserId(checkPhone.getUserId());
                    userMap.setUpdateTime(new DateTime());
                    if(session.getUnionid()!=null){
                        userMap.setUnionId(session.getUnionid());
                    }
                    userService.updateFsUser(userMap);
                }
                else{
                    //写入
                    user=new FsUser();
                    user.setPhone(phoneNoInfo.getPhoneNumber());
//                    user.setNickname("微信用户"+phoneNoInfo.getPhoneNumber().substring(phoneNoInfo.getPhoneNumber().length()-4));
                    user.setNickName("微信用户"+phoneNoInfo.getPhoneNumber().substring(phoneNoInfo.getPhoneNumber().length()-4));
                    user.setAvatar("https://hos-1309931967.cos.ap-chongqing.myqcloud.com/fs/20230725/a848605591384ec29d49773dd58d9345.jpg");
                    user.setStatus(1);
                    user.setMaOpenId(session.getOpenid());
                    user.setCreateTime(new Date());
                    if(session.getUnionid()!=null){
                        user.setUnionId(session.getUnionid());
                    }
                    userService.insertFsUser(user);
                }
            }
            else{
                if(user.getStatus().equals(0)){

                    return R.error("此会员已禁用");
                }
                FsUser userMap=new FsUser();
                userMap.setUserId(user.getUserId());
                if(session.getUnionid()!=null){
                    userMap.setUnionId(session.getUnionid());
                }
                userMap.setMaOpenId(session.getOpenid());
                userMap.setPhone(phoneNoInfo.getPhoneNumber());
                userMap.setUpdateTime(new DateTime());
                userService.updateFsUser(userMap);
            }
            String token = jwtUtils.generateToken(user.getUserId());
//            redisCache.setCacheObject("token:"+user.getUserId(),token,365, TimeUnit.DAYS);
            Map<String,Object> map=new HashMap<>();
            map.put("token",token);
            user.setPhone(encryptPhone(phoneNoInfo.getPhoneNumber()));
            map.put("user",user);
            LoginMaWxParam loginMaWxParam = new LoginMaWxParam();
            loginMaWxParam.setAppId(appId);
            loginMaWxParam.setCompanyId(user.getCompanyId());

            userService.handleFsUserWx(user,loginMaWxParam,session);
            return R.ok(map);
        } catch (WxErrorException e) {
            //this.logger.error(e.getMessage(), e);
            return R.error("授权失败,"+e.getMessage());
        }
    }



    /**
     * 看课互医小程序登陆接口
     */
    @ApiOperation("看课互医小程序登陆接口")
    @PostMapping("/loginCourseHis")
    @Transactional
    public R loginCourseHis( @RequestBody LoginParam param) {

        if (StringUtils.isBlank(param.getCode())) {
            return R.error("code不存在");
        }

        String appid="";

        if(!StringUtil.isNullOrEmpty(param.getAppId())){
            appid=param.getAppId();
        }else {
            FsSysConfig con = configUtil.getSysConfig();
            appid = con.getAppid();
        }

//        final WxMaService wxService = WxMaConfiguration.getMaService(con.getAppid());
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        try {
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(param.getCode());
            this.logger.info(session.getSessionKey());
            this.logger.info(session.getOpenid());
            // 解密
            WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(session.getSessionKey(), param.getEncryptedData(), param.getIv());
            //三端用户同步，先用unionid查询
            FsUser user = new FsUser();
            if (session.getUnionid()!=null){
                user = userService.selectFsUserByUnionid(session.getUnionid());
                if (user==null){
                    user = userService.selectFsUserByCourseOpenId(session.getOpenid());
                }
            }else {
                user = userService.selectFsUserByCourseOpenId(session.getOpenid());
            }

            if(user==null){
                //新用户
                String phoneNumber = phoneNoInfo.getPhoneNumber();


                //查询手机号是否存在，如果存在，更新
                FsUser checkPhone=userService.selectFsUserByPhone(encryptPhone(phoneNumber));
                if (checkPhone==null){
                    checkPhone=userService.selectFsUserByPhone(phoneNumber);
                }
                if(checkPhone!=null){
                    user=checkPhone;
                    FsUser userMap=new FsUser();
                    userMap.setCourseMaOpenId(session.getOpenid());
                    userMap.setUserId(checkPhone.getUserId());
                    userMap.setUpdateTime(new DateTime());
                    if(session.getUnionid()!=null){
                        userMap.setUnionId(session.getUnionid());
                    }
                    userService.updateFsUser(userMap);
                }
                else{
                    //写入
                    user=new FsUser();
                    user.setPhone(phoneNoInfo.getPhoneNumber());
//                    user.setNickname("微信用户"+phoneNoInfo.getPhoneNumber().substring(phoneNoInfo.getPhoneNumber().length()-4));
                    user.setNickName("微信用户"+phoneNoInfo.getPhoneNumber().substring(phoneNoInfo.getPhoneNumber().length()-4));
                    user.setAvatar("https://hos-1309931967.cos.ap-chongqing.myqcloud.com/fs/20230725/a848605591384ec29d49773dd58d9345.jpg");
                    user.setStatus(1);
                    user.setCourseMaOpenId(session.getOpenid());
                    user.setCreateTime(new Date());
                    if(session.getUnionid()!=null){
                        user.setUnionId(session.getUnionid());
                    }
                    userService.insertFsUser(user);
                }
            }
            else{
                if(user.getStatus().equals(0)){

                    return R.error("此会员已禁用");
                }
                FsUser userMap=new FsUser();
                userMap.setUserId(user.getUserId());
                if(session.getUnionid()!=null){
                    userMap.setUnionId(session.getUnionid());
                }
                userMap.setCourseMaOpenId(session.getOpenid());
                userMap.setPhone(phoneNoInfo.getPhoneNumber());
                userMap.setUpdateTime(new DateTime());
                userService.updateFsUser(userMap);
            }
            String token = jwtUtils.generateToken(user.getUserId());
//            redisCache.setCacheObject("token:"+user.getUserId(),token,365, TimeUnit.DAYS);
            Map<String,Object> map=new HashMap<>();
            map.put("token",token);
            user.setPhone(encryptPhone(phoneNoInfo.getPhoneNumber()));
            map.put("user",user);
            FsUserLoginLog log = new FsUserLoginLog();
            log.setCode(param.getCode());
            log.setLoginJson(JSON.toJSONString(param));
//            log.setUserRegisterJson(JSON.toJSONString(jsonMap));
            log.setStatus(1);
            log.setUserId(user.getUserId());
            log.setPhone(user.getPhone());
            log.setMaOpenId(user.getMaOpenId());
            log.setCreateTime(DateUtils.getNowDate());
            fsUserLoginLogMapper.insertFsUserLoginLog(log);
            return R.ok(map);
        } catch (WxErrorException e) {
            //this.logger.error(e.getMessage(), e);
            return R.error("授权失败,"+e.getMessage());
        }
    }
    @ApiOperation("公众号登录")
    @PostMapping("/loginByMp")
    public R loginByMp( @RequestBody LoginParam param) {
        if (StringUtils.isBlank(param.getCode())) {
            return R.error("code不存在");
        }
        try{
            WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(param.getCode());
            WxOAuth2UserInfo wxMpUser = wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
            if (StringUtils.isEmpty(wxMpUser.getUnionId())){
                return R.error("未绑定开放平台");
            }
            FsUser user=userService.selectFsUserByUnionid(wxMpUser.getUnionId());
            if(user!=null){
                FsUser userMap=new FsUser();
                userMap.setUserId(user.getUserId());
                userMap.setNickName(wxMpUser.getNickname());
                userMap.setAvatar(wxMpUser.getHeadImgUrl());
                userMap.setMpOpenId(wxMpUser.getOpenid());
                userMap.setUpdateTime(new DateTime());
                userService.updateFsUser(userMap);
            }
            else{
                //写入
                user=new FsUser();
                user.setNickName(wxMpUser.getNickname());
                user.setAvatar(wxMpUser.getHeadImgUrl());
                user.setStatus(1);
                user.setSex(wxMpUser.getSex());
                user.setMpOpenId(wxMpUser.getOpenid());
                user.setUnionId(wxMpUser.getUnionId());
                user.setCreateTime(new Date());
                userService.insertFsUser(user);
            }
            String token = jwtUtils.generateToken(user.getUserId());
            redisCache.setCacheObject("token:"+user.getUserId(),token,604800, TimeUnit.SECONDS);
            Map<String,Object> map=new HashMap<>();
            map.put("token",token);
            map.put("user",user);

            FsUserLoginLog log = new FsUserLoginLog();
            log.setCode(param.getCode());
            log.setLoginJson(JSON.toJSONString(param));
//            log.setUserRegisterJson(JSON.toJSONString(jsonMap));
            log.setStatus(1);
            log.setUserId(user.getUserId());
            log.setPhone(user.getPhone());
            log.setMaOpenId(user.getMaOpenId());
            log.setCreateTime(DateUtils.getNowDate());
            fsUserLoginLogMapper.insertFsUserLoginLog(log);
            return R.ok(map);
        }
        catch (WxErrorException e){
            if(e.getError().getErrorCode()==40163){
                return R.error(40163,e.getError().getErrorMsg());
            }
            else{
                return R.error("授权失败,"+e.getMessage());
            }
        }

    }

    /**
     * <pre>
     * 获取微信用户信息
     * </pre>
     */
    @Login
    @ApiOperation("获取微信小程序用户信息")
    @PostMapping("/getWeixinInfo")
    public R getWeixinInfo(@RequestBody LoginParam param) {
        FsSysConfig con = configUtil.getSysConfig();
        final WxMaService wxService = WxMaConfiguration.getMaService(con.getAppid());
        try {
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(param.getCode());
            // 用户信息校验
            if (!wxService.getUserService().checkUserInfo(session.getSessionKey(), param.getRawData(), param.getSignature())) {
                return R.error("user check failed");
            }
            // 解密用户信息
            WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(session.getSessionKey(), param.getEncryptedData(), param.getIv());
            FsUser user=userService.selectFsUserByUserId(Long.parseLong(getUserId()));
            user.setNickName(userInfo.getNickName());
            user.setAvatar(userInfo.getAvatarUrl());
            user.setIsWeixinAuth(1);
            userService.updateFsUser(user);
            return R.ok();
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return R.ok("授权成功");
    }

    @Login
    @ApiOperation("注册IM账号和添加好友关系")
    @PostMapping("/bindingImFriend")
    public void createAndBindingImFriend(@RequestParam Long companyUserId) {
        if (ImTypeConfig.IMTYPE== ImTypeEnum.OPENIM){
            openIMService.checkAndImportFriend(companyUserId, getUserId());
        }

    }

}
