package com.zswl.cloud.springBtach.server.core.api.ypp;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zswl.cloud.springBatch.client.model.request.CreateOrder2Request;
import com.zswl.cloud.springBatch.client.model.request.CreateOrdereRquest;
import com.zswl.cloud.springBatch.client.model.request.OrderListRequest;
import com.zswl.cloud.springBtach.server.core.helper.RedisHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.HashMap;

//接口地址 https://apifox.com/apidoc/shared-48c64918-c4ce-49f4-b846-d23855299d7a/doc-2590155
@Component
public class YppApi {

    @Autowired
    RedisHelper redisHelper;


    @Value("${ypp.entId}")
    private String entId;

    @Value("${ypp.appSecret}")
    private String appSecret;

    private final String tokenName = "ypp-token";
    private final String tokenValue = "Bearer ";


    /**
     * 订单简要信息(字段较简洁，用于发起订单支付)
     *
     * @param orderId
     * @return
     */
    public JSONObject order_detail(String orderId) {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/order_detail?id=" + orderId);
        return entries;

    }

    /**
     * 支付订单(支付成功后调用)
     *
     * @param orderId
     * @param mobile
     * @return
     */
    public JSONObject pay_order(String orderId, String mobile) {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/pay_order?id=" + orderId + "&test=&mobile=" + mobile);
        return entries;

    }

    /**
     * 订单列表
     *
     * @param orderListRequest
     * @return
     */
    public JSONObject order_list(OrderListRequest orderListRequest) {
        JSONObject entries = httpPost("https://channel.youpiaopiao.cn/api/movie/list", orderListRequest);
        return entries;

    }

    /**
     * 订单详情
     *
     * @param orderId
     * @return
     */
    public JSONObject get_order(String orderId) {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/get_order?id=" + orderId);
        return entries;

    }

    /**
     * 创建订单 价格跟随有票票
     *
     * @param createOrdereRquest
     * @return
     */
    public JSONObject create_order(CreateOrdereRquest createOrdereRquest) {
        JSONObject entries = httpPost("https://channel.youpiaopiao.cn/api/movie/create_order", createOrdereRquest);
        return entries;

    }

    /**
     * 创建订单自定义价格
     *
     * @param createOrder2Request
     * @return
     */
    public JSONObject create_order2(CreateOrder2Request createOrder2Request) {
        JSONObject entries = httpPost("https://channel.youpiaopiao.cn/api/movie/create_order2", createOrder2Request);
        return entries;

    }

    /**
     * 热映电影（pageIndex从0开始）
     *
     * @param
     * @return
     */
    public JSONObject movies(String cityId, String pageIndex, String pageSize) {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/movies?cityId=" + cityId + "&pageIndex=" + pageIndex + "&pageSize=" + pageSize);
        return entries;

    }

    /**
     * 即将上映电影
     *
     * @param cityId
     * @return
     */
    public JSONObject coming_movies(String cityId) {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/coming_movies?cityId=" + cityId);
        return entries;

    }

    /**
     * 获取电影详情
     *
     * @param movieId
     * @return
     */
    public JSONObject movie_detail(String movieId) {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/movie_detail?movieId=" + movieId);
        return entries;

    }

    /**
     * 获取电影的影院列表
     *
     * @param movieId
     * @return
     */
    public JSONObject movie_cinemas(String cityId, String movieId) {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/movie_cinemas?cityId=" + cityId + "&movieId=" + movieId);
        return entries;

    }

    /**
     * 获取影院列表
     *
     * @param cityId
     * @param districtId
     * @param brandId
     * @param lat
     * @param lng
     * @return
     */

    public JSONObject cinemas(String cityId, String districtId, String brandId, String lat, String lng) {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/cinemas?cityId=" + cityId + "&districtId=" + districtId + "&brandId=" + brandId + "&lat=" + lat + brandId + "&lng=" + lng);
        return entries;

    }

    /**
     * 获取影院过滤列表
     *
     * @param cityId
     * @return
     */
    public JSONObject cinema_filter(String cityId) {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/cinema_filter?cityId=" + cityId);
        return entries;

    }

    /**
     * 获取城市列表
     *
     * @return
     */
    public JSONObject get_citys() {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/get_citys");
        return entries;

    }


    /**
     * 获取影院某个电影的排片列表
     *
     * @param movieId
     * @param cinemaId
     * @param currentMovieId
     * @return
     */
    public JSONObject cinema_shows(String movieId, String cinemaId, String currentMovieId) {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/cinema_shows?cinemaId=cinemaId" + cinemaId + "&movieId=" + movieId + "&currentMovieId=" + currentMovieId);
        return entries;
    }

    /**
     * 获取电影场次（座位）详情
     *
     * @param sessionId
     * @param cinemaId
     * @param movieId
     * @param sid
     * @return
     */
    public JSONObject session(String sessionId, String cinemaId, String movieId, String sid) {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/session?sessionId=" + sessionId + "&cinemaId=" + cinemaId + "&movieId=" + movieId + "&sid=" + sid);
        return entries;
    }

    /**
     * 获取广告列表
     *
     * @return
     */
    public JSONObject ad_configs() {
        JSONObject entries = httpGet("https://channel.youpiaopiao.cn/api/movie/ad_configs");
        return entries;
    }

    private JSONObject httpGet(String url) {
        String token = getoken();
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.header("Authorization", token).execute();
        String body = execute.body();
        JSONObject entries = JSONUtil.parseObj(body);
        Integer code = (Integer) entries.get("code");
        Assert.isTrue(execute.isOk(), "飘飘响应错误");
        Assert.isTrue(code.equals(0), String.valueOf(entries.get("message")));
        return JSONUtil.parseObj(body);

    }

    private JSONObject httpPost(String url, Object body) {

        HttpRequest post = HttpUtil.createPost(url);
        if (!url.contains("api/oauth/token")) {
            String token = getoken();
            post.header("Authorization", token);
        }
        HttpResponse execute = post.body(JSONUtil.toJsonStr(body)).execute();
        String response = execute.body();
        JSONObject entries = JSONUtil.parseObj(response);
        Integer code = (Integer) entries.get("code");
        Assert.isTrue(execute.isOk(), "票票响应错误");
        Assert.isTrue(code.equals(0), String.valueOf(entries.get("message")));
        return entries;
    }

    private String getoken() {
        Object value = redisHelper.getValue(tokenName);
        if (!ObjectUtil.isEmpty(value)) {
            return String.valueOf(value);
        }
        HashMap<String, String> para = new HashMap<>();
        para.put("entId", entId);
        para.put("appSecret", appSecret);
        JSONObject http = httpPost("https://channel.youpiaopiao.cn/api/oauth/token", para);
        String access_token = (String) http.getByPath("data.access_token");
        String token = tokenValue + access_token;
        redisHelper.cacheValue(tokenName, token, 82800);
        return token;
    }

}
