package com.kmall.api.api;

import com.alibaba.fastjson.JSONObject;
import com.kmall.api.annotation.IgnoreAuth;
import com.kmall.api.entity.FullUserInfo;
import com.kmall.api.entity.StoreVo;
import com.kmall.api.entity.UserInfo;
import com.kmall.api.entity.UserVo;
import com.kmall.api.service.ApiStoreService;
import com.kmall.api.service.ApiUserService;
import com.kmall.api.service.TokenService;
import com.kmall.api.util.ApiBaseAction;
import com.kmall.api.util.ApiUserUtils;
import com.kmall.api.util.CommonUtil;
import com.qiniu.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * API登录授权
 *
 * @author Scott
 * @email
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/api/auth")
public class ApiAuthController extends ApiBaseAction {
    private Logger logger = Logger.getLogger(getClass());
    @Autowired
    private ApiUserService userService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ApiStoreService apiStoreService;

    /**
     * 登录
     */
    @IgnoreAuth
    @PostMapping("login_by_weixin")
    public Object loginByWeixin() {
        Map<String, Object> resultObj = new HashMap();
        //
        JSONObject jsonParam = this.getJsonRequest();
        FullUserInfo fullUserInfo = null;
        Long storeId = jsonParam.getLong("storeId");
        String code = "";
        if (!StringUtils.isNullOrEmpty(jsonParam.getString("code"))) {
            code = jsonParam.getString("code");
        }
        if (null != jsonParam.get("userInfo")) {
            fullUserInfo = jsonParam.getObject("userInfo", FullUserInfo.class);
        }
        //
        UserInfo userInfo = fullUserInfo.getUserInfo();
        //获取openid
        String requestUrl = ApiUserUtils.getWebAccess(code);//通过自定义工具类组合出小程序需要的登录凭证 code
        logger.info("》》》组合token为：" + requestUrl);
        JSONObject sessionData = CommonUtil.httpsRequest(requestUrl, "GET", null);

        if (null == sessionData || StringUtils.isNullOrEmpty(sessionData.getString("openid"))) {
            return toResponsFail("登录失败");
        }
        //验证用户信息完整性
        String sha1 = CommonUtil.getSha1(fullUserInfo.getRawData() + sessionData.getString("session_key"));
        if (!fullUserInfo.getSignature().equals(sha1)) {
            return toResponsFail("登录失败");
        }
        Date nowTime = new Date();
        UserVo userVo = userService.queryByOpenId(sessionData.getString("openid"));
        userInfo.setNickName(filterEmoji(userInfo.getNickName()));;
        if (null == userVo) {
            userVo = new UserVo();
            userVo.setPassword(sessionData.getString("openid"));
            userVo.setRegister_time(nowTime);
            userVo.setRegister_ip(this.getClientIp());
            userVo.setLast_login_ip(userVo.getRegister_ip());
            userVo.setLast_login_time(userVo.getRegister_time());
            userVo.setWeixin_openid(sessionData.getString("openid"));
            userVo.setAvatar(userInfo.getAvatarUrl());
            userVo.setGender(userInfo.getGender()); // //性别 0：未知、1：男、2：女
            userVo.setNickname(userInfo.getNickName());
            userVo.setUser_level_id(1);
            userService.save(userVo);

        } else {
            userVo.setLast_login_ip(this.getClientIp());
            userVo.setLast_login_time(nowTime);
            userVo.setAvatar(userInfo.getAvatarUrl());
            userVo.setGender(userInfo.getGender()); // //性别 0：未知、1：男、2：女
            userService.update(userVo);
        }

        Map<String, Object> tokenMap = tokenService.createAndUpdateToken(userVo.getWeixin_openid(), storeId,userVo.getId());
        String token = MapUtils.getString(tokenMap, "token");

        if (null == userInfo || StringUtils.isNullOrEmpty(token)) {
            return toResponsFail("登录失败");
        }

        resultObj.put("token", token);
        resultObj.put("userInfo", userInfo);
        resultObj.put("userId", userVo.getId());
        return toResponsSuccess(resultObj);
    }
    /**
     * 将emoji表情替换成空串
     * @param source
     * @return 过滤后的字符串
     **/
    public static String filterEmoji(String source) {
        if (source != null && source.length() > 0) {
            return source.replaceAll("[\ud800\udc00-\udbff\udfff\ud800-\udfff]", "");
        } else {
            return source;
        }
    }



    /**
     * 根据经纬度选择门店
     */
    @GetMapping("nearbyList")
    @IgnoreAuth
    public Object nearbyList(@RequestParam BigDecimal latitude, @RequestParam BigDecimal longitude) {
        Map param = new HashMap();
        param.put("latitude", latitude);
        param.put("longitude", longitude);
        List<StoreVo> storeVoList = apiStoreService.queryNearbyList(param);
        if (null != storeVoList && storeVoList.size() > 0 && null != getOpenId()) {
            tokenService.createAndUpdateToken(getOpenId(), storeVoList.get(0).getId(),getUserId());
        }
        return toResponsSuccess(storeVoList);
    }

    /**
     * 根据城市获取门店
     */
    @GetMapping("storeListByCity")
    @IgnoreAuth
    public Object storeListByCity(@RequestParam String provinceName, @RequestParam String cityName, @RequestParam String countyName) {
        Map param = new HashMap();
        param.put("province_name", provinceName);
        param.put("city_name", cityName);
        param.put("county_name", countyName);

        List<StoreVo> storeVoList = apiStoreService.queryList(param);
        return toResponsSuccess(storeVoList);
    }

    /**
     * 选择门店Id
     */
    @PostMapping("chooseStoreId")
    public Object chooseStoreId() {
        JSONObject jsonParam = this.getJsonRequest();
        String openId = getOpenId();
        Long storeId = jsonParam.getLong("storeId");
        //
        Map<String, Object> resultObj = new HashMap();
        Map<String, Object> tokenMap = tokenService.createAndUpdateToken(openId, storeId,getUserId());
        String token = MapUtils.getString(tokenMap, "token");
        resultObj.put("token", token);
        return toResponsSuccess(resultObj);
    }
}
