package com.yutou.bilibili.Services.impl;

import com.yutou.bilibili.Services.IUserService;
import com.yutou.bilibili.Tools.AppTools;
import com.yutou.bilibili.mybatis.dao.UBiliUpDao;
import com.yutou.bilibili.mybatis.dao.UUserDao;
import com.yutou.bilibili.mybatis.model.UBiliUp;
import com.yutou.bilibili.mybatis.model.UBiliUpExample;
import com.yutou.bilibili.mybatis.model.UUser;
import com.yutou.bilibili.mybatis.model.UUserExample;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

@Service("UserService")
public class UserServiceImpl implements IUserService {
    @Resource
    UUserDao userDao;
    @Resource
    UBiliUpDao upDao;
    @Override
    public boolean login(String uname, String password) {
        UUserExample example=new UUserExample();
        String _password=AppTools.getMD5 (password);
        example.createCriteria().andUserEqualTo(uname).andPasswordEqualTo(_password);
        return !userDao.selectByExample(example).isEmpty();
    }

    @Override
    public boolean reg(UUser user) {
        if(checkUser(user.getUser())){
            return false;
        }
        user.setPassword(AppTools.getMD5 (user.getPassword()));
        return  userDao.insert(user)>0;
    }

    @Override
    public boolean addUp(UBiliUp up) {
        if(checkUP(up.getUid(),up.getRoomid())){
            return false;
        }
        return upDao.insert(up)>0;
    }

    @Override
    public boolean removeUp(UUser user, UBiliUp up) {
        if(checkUP(up.getUid(),up.getRoomid())){
            return false;
        }
        return upDao.deleteByPrimaryKey(up.getId())>0;
    }

    @Override
    public boolean update(UUser user) {
        return userDao.updateByPrimaryKey(user)>0;
    }

    @Override
    public UUser getUser(String uname) {
        UUserExample example=new UUserExample();
        example.createCriteria().andUserEqualTo(uname);
        List<UUser> list=userDao.selectByExample(example);
        if(list.isEmpty()){
            return null;
        }else{
            return list.get(0);
        }
    }

    @Override
    public UUser getUserToUid(Integer uid) {
        return userDao.selectByPrimaryKey(uid);
    }

    @Override
    public List<UBiliUp> getUserUp(int uid) {
        UBiliUpExample example=new UBiliUpExample();
        example.createCriteria().andUidEqualTo(uid);
        return upDao.selectByExample(example);
    }

    @Override
    public UUser getUserToToken(String token) {
        if(StringUtils.isEmpty(token)){
            return null;
        }
        UUserExample example=new UUserExample();
        example.createCriteria().andLogintokenEqualTo(token);
        List<UUser> list=userDao.selectByExample(example);
        if(list.isEmpty()){
            return null;
        }else{
            return list.get(0);
        }
    }

    public boolean checkUser(String uname){
        UUserExample example=new UUserExample();
        example.createCriteria().andUserEqualTo(uname);
        return !userDao.selectByExample(example).isEmpty();
    }
    public boolean checkUP(int uid,int roomId){
        UBiliUpExample example=new UBiliUpExample();
        example.createCriteria().andUidEqualTo(uid).andRoomidEqualTo(roomId);
        return !upDao.selectByExample(example).isEmpty();
    }
}
