package com.bohuikeji.delivery.backstage.controller.api.v2;

import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.delivery.backstage.constants.WxAppEm;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.wx.WeChatJsConfig;
import com.bohuikeji.delivery.backstage.util.WxUtils;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = {"小程序分享相关接口"})
@RestController
@RequestMapping("/api/v2/share")
@RequiredArgsConstructor
public class ShareV2Api extends ApiController {

    @ApiOperation(value = "获取客户端小程序分享连接",notes = "使用方法详见微信文档：https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/url-link.html")
    @GetMapping("/client/mp/get/link")
    public APIEntity getClientLink(@ApiParam(name = "path",value = "小程序页面路径(选填，不传跳首页)") @RequestParam(required = false) String path){
        return APIEntity.success(WxUtils.getMpShareLink(WxAppEm.MP_CLIENT,path,null).getString("url_link"));
    }

    /**
     * 获取骑手小程序分享连接
     * @param path
     * @param query
     * @return
     */
    @ApiOperation(value = "获取骑手小程序分享连接",notes = "使用方法详见微信文档：https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/url-link.html")
    @GetMapping("/courier/mp/get/link")
    public APIEntity<String> getCourierLink(@ApiParam(name = "path",value = "小程序页面路径(选填，不传跳首页)") @RequestParam(required = false) String path,
                                            @ApiParam(name = "query",value = "需要携带的参数(选填)") @RequestParam(required = false) String query){
        return APIEntity.success(WxUtils.getMpShareLink(WxAppEm.MP_COURIER,path,query).getString("url_link"));
    }

    /**
     * 获取电马小程序分享连接
     * @param path
     * @param query
     * @return
     */
    @ApiOperation(value = "获取电马小程序分享连接",notes = "使用方法详见微信文档：https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/url-link.html")
    @GetMapping("/dianma/mp/get/link")
    public APIEntity<String> getDianMaLink(@ApiParam(name = "serviceStationName",value = "需要携带的参数(必填)") @RequestParam(required = false) String serviceStationName){
        String query=null;
        switch (serviceStationName){
            case "首页":
                query = "user_type_id=5";break;
            case "服务站":
                query = "user_type_id=5&other=shop_d&id=1520";break;
            case "城北站":
                query = "user_type_id=5&other=shop_d&id=1521";break;
            case "旗舰站":
                query = "user_type_id=5&other=shop_d&id=1519";break;
            case "锦江店":
                query = "user_type_id=5&other=shop_d&id=1517";break;
            case "分公司":
                query = "user_type_id=5&other=shop_d&id=1505";break;
            case "族桥站":
                query = "user_type_id=5&other=shop_d&id=1326";break;
            default:
                log.error("客户端参数错误，无法识别的参数值：[{}]",serviceStationName);
                return APIEntity.fail("无法识别参数[orderStatus="+serviceStationName+"]");
        }
        String path = "/pages/ad_page/ad_page";
        return APIEntity.success(WxUtils.getMpShareLink(WxAppEm.MP_DIANMA,path,query).getString("url_link"));
    }

    /**
     * 获取公众号accessToken
     * @return
     */
    @ApiOperation(value = "获取公众号token")
    @GetMapping("/client/dzh/get/token")
    public String getAccessTokenGZH() {
        return WxUtils.getAccessToken(WxAppEm.GZH_TCJS);
    }

    /**
     * 获取js-api-ticket
     * @return
     */
    @ApiOperation(value = "获取公众号js-api-ticket")
    @GetMapping("/client/gzh/get/ticket")
    public String getJsApiTicket() {
        return WxUtils.getJsApiTicket(WxAppEm.GZH_TCJS);
    }

    /**
     * 获取微信JS配置信息
     * @param currentWebPageUrl
     * @return
     */
    @ApiOperation(value = "获取微信JS配置信息")
    @GetMapping(value = "/client/gzh/get/Config")
    public APIEntity<WeChatJsConfig> getWeChatJsConfig(@ApiParam(value = "当前网页路径URL", required = true) @RequestParam String currentWebPageUrl) {
        return APIEntity.success(WxUtils.getJsConfig(WxAppEm.GZH_TCJS,currentWebPageUrl));
    }

}
