package cn.edu.ecnu.stu.bookstore;

import cn.edu.ecnu.stu.bookstore.component.Constants;
import cn.edu.ecnu.stu.bookstore.component.Result;
import cn.edu.ecnu.stu.bookstore.pojo.User;
import cn.edu.ecnu.stu.bookstore.utils.ImageUtil;
import cn.edu.ecnu.stu.bookstore.util.RequestUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.util.*;

@SpringBootTest
public class OrderTest {

    private String token;

    private final String storeId = "store" + UUID.randomUUID();

    private String username;

    public void init() {
        if(username != null)
            return;
        String url = Constants.URL_PREFIX + "/auth/register";
        Map<String, Object> map = new HashMap<>();
        username = UUID.randomUUID().toString();
        String password = username + "x";
        map.put("username", username);
        map.put("password", password);
        Result result = RequestUtil.post(url, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        url = Constants.URL_PREFIX + "/auth/login";
        result = RequestUtil.post(url, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        JSONObject data = (JSONObject) result.getData();
        token = data.getString("token");
        Integer userId = data.getObject("user", User.class).getId();

        url = Constants.URL_PREFIX + "/seller/create_store";
        map = new HashMap<>();
        map.put("storeId", storeId);
        result = RequestUtil.post(url, map, token);
        assert result != null && Constants.SUCCESS.equals(result.getCode());

        result = RequestUtil.post(url, map, token);
        assert result != null && Constants.CLIENT_ERROR.equals(result.getCode());

        url = Constants.URL_PREFIX + "/seller/add_book";
        String bookId = UUID.randomUUID().toString();
        String title = "title" + UUID.randomUUID();
        String author = "author" + UUID.randomUUID();
        String publisher = "publisher" + UUID.randomUUID();
        BigDecimal price = new BigDecimal("29.88");
        String authorIntro = "authorIntro" + UUID.randomUUID();
        String bookIntro = "bookIntro" + UUID.randomUUID();
        String content = "content" + UUID.randomUUID();
        List<String> tags = Arrays.asList("tag" + UUID.randomUUID(), "tag" + UUID.randomUUID());
        List<String> pictures = Arrays.asList(ImageUtil.convertImageToBase64Str("ff3c65cd-497e-4b17-aa55-5882a38ed2fd.png"));
        map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("bookId", bookId);
        map.put("title", title);
        map.put("author", author);
        map.put("publisher", publisher);
        map.put("price", price);
        map.put("authorIntro", authorIntro);
        map.put("bookIntro", bookIntro);
        map.put("content", content);
        map.put("tags", tags);
        map.put("pictures", pictures);
        result = RequestUtil.post(url, map, token);
        assert result != null && Constants.SUCCESS.equals(result.getCode());

        map.put("bookId", bookId + "x");
        List<String> bookIds = Arrays.asList(bookId, bookId + "x");
        result = RequestUtil.post(url, map, token);
        assert result != null && Constants.SUCCESS.equals(result.getCode());

        url = Constants.URL_PREFIX + "/seller/add_stock_level";
        map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("addStockLevel", 100);
        for (String id : bookIds) {
            map.put("bookId", id);
            result = RequestUtil.post(url, map, token);
            assert result != null && Constants.SUCCESS.equals(result.getCode());
        }

        url = Constants.URL_PREFIX + "/buyer/new_order";
        map = new HashMap<>();
        map.put("storeId", storeId);
        List<Map<String, Object>> list = new LinkedList<>();
        int c = 1;
        for (String bookId1 : bookIds) {
            HashMap<String, Object> map0 = new HashMap<>();
            map0.put("id", bookId1);
            map0.put("count", c++);
            list.add(map0);
        }
        map.put("books", list);
        result = RequestUtil.post(url, map, token);
        assert result != null && Constants.SUCCESS.equals(result.getCode());
    }

    @Test
    public void testGetOrderList() {
        init();
        String url = Constants.URL_PREFIX + "/order/";
        Result result = RequestUtil.get(url, null, token);
        assert result != null && Constants.SUCCESS.equals(result.getCode());
    }

    @Test
    public void testCancelOrder() {
        init();
        String url = Constants.URL_PREFIX + "/order/";
        Result result = RequestUtil.get(url, null, token);
        assert result != null && Constants.SUCCESS.equals(result.getCode());

        JSONArray data = (JSONArray) result.getData();
        JSONObject object = (JSONObject)data.get(0);
        String orderId = object.getString("orderId");

        url = Constants.URL_PREFIX + "/order/cancel_order";
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        result = RequestUtil.post(url, map, token);
        assert result != null && Constants.SUCCESS.equals(result.getCode());
    }
}
