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.mapper.OrderMapper;
import cn.edu.ecnu.stu.bookstore.pojo.Order;
import cn.edu.ecnu.stu.bookstore.pojo.OrderStatus;
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.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

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

@SpringBootTest
public class SellerTest {

    private String token;

    private String storeId;

    private String bookId;

    private Integer userId;

    @Autowired
    private OrderMapper orderMapper;

    public void init() {
        if(token != null)
            return;
        String url = Constants.URL_PREFIX + "/auth/register";
        Map<String, Object> map = new HashMap<>();
        String 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");
        userId = data.getObject("user", User.class).getId();
    }

    @Test
    public void testCreateStore() {
        init();
        String url = Constants.URL_PREFIX + "/seller/create_store";
        HashMap<String, Object> map = new HashMap<>();
        storeId = "store" + UUID.randomUUID();
        map.put("storeId", storeId);
        Result 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());

    }

    @Test
    public void testAddBook() {
        init();
        testCreateStore();
        bookId = "book" + UUID.randomUUID();
        String url = Constants.URL_PREFIX + "/seller/add_book";
        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"));
        HashMap<String, Object> 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 result = RequestUtil.post(url, map, token);
        assert result != null && Constants.SUCCESS.equals(result.getCode());

    }

    @Test
    public void testAddStockLevel() {
        init();
        testAddBook();
        String url = Constants.URL_PREFIX + "/seller/add_stock_level";
        HashMap<String, Object> map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("bookId", bookId);
        map.put("addStockLevel", 10);
        Result result = RequestUtil.post(url, map, token);
        assert result != null && Constants.SUCCESS.equals(result.getCode());
    }

    @Test
    public void testSendGoods() {
        testAddStockLevel();
        String orderId = UUID.randomUUID().toString();

        Order order = new Order(orderId, userId, null, null, new BigDecimal("123.23"), OrderStatus.WAIT_SEND, storeId, null);
        String body = JSON.toJSONString(order);
        String url = Constants.URL_PREFIX + "/order/add";
        Result result = RequestUtil.post(url, body, token);
        assert result != null && Constants.SUCCESS.equals(result.getCode());
        url = Constants.URL_PREFIX + "/seller/send_goods";
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        result = RequestUtil.post(url, map, token);
        assert result != null && Constants.SUCCESS.equals(result.getCode());
    }
}
