package com.copy.trade.service.impl;

import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copy.trade.dao.MessageMapper;
import com.copy.trade.entity.po.Customer;
import com.copy.trade.entity.po.FollowConfig;
import com.copy.trade.entity.po.Message;
import com.copy.trade.entity.vo.MessageVo;
import com.copy.trade.entity.vo.NoticeVo;
import com.copy.trade.enums.FollowConfigEnum;
import com.copy.trade.service.ICustomerService;
import com.copy.trade.service.IFollowConfigService;
import com.copy.trade.service.IMessageService;
import com.copy.trade.util.DogeContractUtils;
import com.copy.trade.util.TraderConfiguration;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Value("${notice.list}")
    private String listUrl;
    
    @Value("${notice.detail}")
    private String detailUrl;
    
    @Value("${notice.menuId}")
    private String menuId;
    
    @Value("${notice.language}")
    private String language;
	
	@Autowired
	private ICustomerService customerServiceImpl;
    
    @Autowired
    private IFollowConfigService followConfigServiceImpl;
	
	@Override
	public Page<MessageVo> listByCustomerId(Long memberId, Page page) {
		// TODO Auto-generated method stub
		Customer customer = customerServiceImpl.getByMemberId(memberId);
		List<MessageVo> list = ((MessageMapper)baseMapper).listByCustomerId(page, customer.getId());
		list.forEach(messageVo -> {
			genMessage(messageVo);
		});
		page.setRecords(list);
		((MessageMapper)baseMapper).setRead(customer.getId());
		return page;
	}

	@Override
	public List<Message> listUnreadByCustomerId(Long memberId) {
		// TODO Auto-generated method stub
		Customer customer = customerServiceImpl.getByMemberId(memberId);
		List<Message> list = ((MessageMapper)baseMapper).listUnreadByCustomerId(customer.getId());
		return list;
	}
	
	private void genMessage(MessageVo messageVo) {
		String title = messageVo.getMessageType().getDes();
		String symbol = messageVo.getSymbol();
		if(!StringUtils.isEmpty(symbol)) {
			symbol = symbol.substring(0, 3);
		}
		String traderName = "老师";
		if(!StringUtils.isEmpty(messageVo.getContent())) {
			if(!NumberUtils.isParsable(messageVo.getContent())) {
				traderName = messageVo.getContent();
			} else {
				Customer trader = customerServiceImpl.getById(Integer.valueOf(messageVo.getContent()));
				traderName = trader.getNickname();
				FollowConfig followConfig = followConfigServiceImpl.getConfig(FollowConfigEnum.SPECIAL_TRADER_CODE.getNameEn());
		        boolean isSpecialTrader = trader.getTraderLevelId() != null && trader.getTraderLevelId() >= Integer.parseInt(followConfig.getConfigContent());
				if(isSpecialTrader) {
					traderName = TraderConfiguration.getSpecailName(trader.getMemberId(),trader.getNickname());
				}
			}
		}
		String content;
		switch(messageVo.getMessageType()) {
			case OPEN_SUCCESS:
			case CLOSE_SUCCESS:
				content = "跟随" + traderName +  title + "," + symbol + "成交价格: " + messageVo.getDealPrice();
				break;
			case OPEN_FAILURE:
				content = "跟随" + traderName +  title +"," + "余额不足";
				break;
			case FORCE_CLOSE:
				content = title + "," + symbol +  "强平价格: " + messageVo.getDealPrice();
				break;
			case POSITION_PROFIT:
			case POSITION_LOSS:
			case ACCOUNT_PROFIT:
			case ACCOUNT_LOSS:
				content = title + "," + symbol +  "成交价格: " + messageVo.getDealPrice();
				break;
			case UNFOLLOW_REMIND:
				content = "自动停止跟随交易员: " + traderName;
				break;
			default:
				content = "";
				break;
		}
		messageVo.setTitle(title);
		messageVo.setContent(content);
	}

	@Override
	public List<NoticeVo> noticeList() {
		return DogeContractUtils.getNotice(listUrl,detailUrl,menuId,language);
	}
	


}
