package cn.edu.ecnu.stu.bookstore.service.impl;

import cn.edu.ecnu.stu.bookstore.component.AppException;
import cn.edu.ecnu.stu.bookstore.component.Constants;
import cn.edu.ecnu.stu.bookstore.mapper.BookMapper;
import cn.edu.ecnu.stu.bookstore.mapper.OrderMapper;
import cn.edu.ecnu.stu.bookstore.mapper.StoreMapper;
import cn.edu.ecnu.stu.bookstore.pojo.*;
import cn.edu.ecnu.stu.bookstore.service.SellerService;
import cn.edu.ecnu.stu.bookstore.utils.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    @Lazy
    private SellerService sellerService;

    public void createStore(Store store) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        store.setSellerId(user.getId());
        if(storeMapper.checkStoreExist(store.getStoreId()) != 0)
            throw new AppException(Constants.CLIENT_ERROR, Constants.STORE_ID_ERROR);
        storeMapper.insert(store);
    }

    public void addBook(Book book) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int userId = user.getId();
        if(storeMapper.checkStore(userId, book.getStoreId()) == 0)
            throw new AppException(Constants.CLIENT_ERROR, Constants.STORE_NON_EXIST_ERROR);

        List<String> tags = book.getTags();
        List<String> pictures = book.getPictures();
        pictures = pictures.stream().map(ImageUtil::convertBase64StrToImage).collect(Collectors.toList());
        sellerService.insertBook(book, tags, pictures);
    }

    @Transactional
    public void insertBook(Book book, List<String> tags, List<String> pictures) {
        bookMapper.insert(book);
        bookMapper.insertBookDetail(book);
        bookMapper.insertTags(book.getBookId(), tags);
        bookMapper.insertPictures(book.getBookId(), pictures);
    }

    @Override
    public void addStockLevel(String storeId, String bookId, int addStockLevel) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int userId = user.getId();
        if(storeMapper.checkStore(userId, storeId) == 0)
            throw new AppException(Constants.CLIENT_ERROR, Constants.STORE_NON_EXIST_ERROR);

        if(bookMapper.checkBook(storeId, bookId) == 0)
            throw new AppException(Constants.CLIENT_ERROR, Constants.BOOK_ERROR);
        bookMapper.addStockLevel(storeId, bookId, addStockLevel);
    }

    @Override
    public void sendGoods(String orderId) {
        Order order = orderMapper.selectById(orderId);
        if(order == null)
            throw new AppException(Constants.CLIENT_ERROR, Constants.ORDER_NON_EXIST_ERROR);
        String storeId = order.getStoreId();
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(storeMapper.checkStore(user.getId(), storeId) != 1)
            throw new AppException(Constants.CLIENT_ERROR, Constants.AUTHORITY_ERROR);
        if(!order.getStatus().equals(OrderStatus.WAIT_SEND))
            throw new AppException(Constants.CLIENT_ERROR, Constants.STATUS_ERROR);
        orderMapper.updateOrderStatus(orderId, OrderStatus.WAIT_RECEIVE);
    }

}
