package com.haha.sdk.api;

import com.haha.sdk.HahaClient;
import com.haha.sdk.exception.HahaException;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 订单相关API
 * 
 * @author haha-sdk
 * @version 1.0.0
 */
@Slf4j
public class OrderApi extends BaseApi {
    
    public OrderApi(HahaClient client) {
        super(client);
    }
    
    /**
     * 查询识别结果
     * 
     * @param orderId 订单编号（与activityId二选一）
     * @param activityId 活动编号（与orderId二选一）
     * @return 识别结果
     * @throws HahaException 调用失败时抛出
     */
    public Map<String, Object> getRecognizeResult(String orderId, String activityId) throws HahaException {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", client.getAccessToken());
        if (orderId != null && !orderId.isEmpty()) {
            params.put("order_id", orderId);
        }
        if (activityId != null && !activityId.isEmpty()) {
            params.put("activity_id", activityId);
        }
        
        Map<String, Object> result = post("/order/recResult", params);
        return (Map<String, Object>) result.get("data");
    }
    
    /**
     * 查询订单详情
     * 
     * @param orderId 订单编号（与activityId二选一）
     * @param activityId 活动编号（与orderId二选一）
     * @return 订单详情
     * @throws HahaException 调用失败时抛出
     */
    public Map<String, Object> queryOrder(String orderId, String activityId) throws HahaException {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", client.getAccessToken());
        if (orderId != null && !orderId.isEmpty()) {
            params.put("order_id", orderId);
        }
        if (activityId != null && !activityId.isEmpty()) {
            params.put("activity_id", activityId);
        }
        
        Map<String, Object> result = post("/order/getOrderInfo", params);
        return (Map<String, Object>) result.get("data");
    }
    
    /**
     * 设置订单支付状态
     * 
     * @param orderId 哈哈平台订单号
     * @return 是否成功
     * @throws HahaException 调用失败时抛出
     */
    public boolean setPayStatus(String orderId) throws HahaException {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", client.getAccessToken());
        params.put("order_id", orderId);
        
        post("/order/setOrderStatus", params);
        return true;
    }
    
    /**
     * 获取开门前后图片或视频
     * 
     * @param orderId 订单编号（与activityId二选一）
     * @param activityId 活动编号（与orderId二选一）
     * @return 媒体文件信息
     * @throws HahaException 调用失败时抛出
     */
    public Map<String, Object> getOrderMedia(String orderId, String activityId) throws HahaException {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", client.getAccessToken());
        if (orderId != null && !orderId.isEmpty()) {
            params.put("order_id", orderId);
        }
        if (activityId != null && !activityId.isEmpty()) {
            params.put("activity_id", activityId);
        }
        
        Map<String, Object> result = post("/order/getorderspic", params);
        return (Map<String, Object>) result.get("data");
    }
    
    /**
     * 查询识别或订单回调结果
     * 查询哈哈平台是否成功推送识别结果或订单通知给商户
     * 
     * @param type 识别类型： REC识别结果回调，ORDER 订单结果回调
     * @param actId 活动编号
     * @return 回调状态信息
     * @throws HahaException 调用失败时抛出
     */
    public Map<String, Object> getCallbackInfo(String type, String actId) throws HahaException {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", client.getAccessToken());
        params.put("type", type);
        params.put("act_id", actId);
        
        Map<String, Object> result = post("/order/getCallbackInfo", params);
        return (Map<String, Object>) result.get("data");
    }
    
    /**
     * 获取静态柜分层图片与识别结果
     * 获取静态货柜的分层图片和每层的识别结果（适用于静态识别柜）
     * 
     * @param orderId 订单编号（与activityId二选一）
     * @param activityId 活动编号（与orderId二选一）
     * @return 分层识别结果
     * @throws HahaException 调用失败时抛出
     */
    public Map<String, Object> getLayerRecognize(String orderId, String activityId) throws HahaException {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", client.getAccessToken());
        if (orderId != null && !orderId.isEmpty()) {
            params.put("order_id", orderId);
        }
        if (activityId != null && !activityId.isEmpty()) {
            params.put("activity_id", activityId);
        }
        
        Map<String, Object> result = post("/order/recResultLayers", params);
        return (Map<String, Object>) result.get("data");
    }
}
