import time

from flask import request, Blueprint, Response

from util import json_util
from service.ecommerce.vova import vova_merchant

order = Blueprint('order', __name__)


@order.route('/list_unhandled_order/<platform>', methods=['GET'])
def list_unhandled_order(platform):
    api_token = request.args.get("apiToken")
    if not api_token:
        raise ValueError("Invalid params")

    return response_json_data(vova_merchant.list_unhandled_order(api_token))


@order.route('/list_refund_order/<platform>', methods=['GET'])
def list_refund_order(platform):
    api_token = request.args.get("apiToken")
    user = request.args.get("user")
    password = request.args.get("password")
    cookies = request.args.get("cookies")
    start_time = request.args.get('startTime')
    end_time = request.args.get('endTime')
    if not api_token or not user or not password or not cookies:
        raise ValueError("Invalid params")

    if not start_time:
        start_time = time.strftime("%Y-%m-%d", time.localtime())
    return response_json_data(vova_merchant.list_refund_order(user, password, api_token, cookies, start_time, end_time))


def response_json_data(response):
    assert not isinstance(response, str)
    return Response(json_util.obj2json(response), mimetype='application/json')