package com.reyun.service.impl;

import com.reyun.model.Account;
import com.reyun.model.DeviceWhiteList;
import com.reyun.repository.AccountRepository;
import com.reyun.repository.DeviceWhiteListRepository;
import com.reyun.service.DeviceWhiteListService;
import com.reyun.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class DeviceWhiteListServiceImpl implements DeviceWhiteListService {

    @Autowired
    AccountRepository accountRepository;

    @Autowired
    private DeviceWhiteListRepository deviceWhiteListRepository;

    @Override
    public Object create(Account account, DeviceWhiteList resource) {
        Account rootAccount = accountRepository.findOne(account.getRootParent());
        resource.setAccountId(rootAccount.getId());
        HashMap<String, Boolean> valid = valid(account, resource, false);
        if(valid.size()>0){
            return valid;
        }
        return deviceWhiteListRepository.save(resource);
    }

    @Override
    public HashMap<String, Boolean> valid(Account account, DeviceWhiteList resource, Boolean isUpdate) {

        HashMap<String, Boolean> hashMap = new HashMap<>();
        List<DeviceWhiteList> byAccountAndName = deviceWhiteListRepository.findByAccountAndName(resource.getAccountId(), resource.getDeviceName(), resource.getPlatform());
        List<DeviceWhiteList> byAccountAndIdfa = deviceWhiteListRepository.findByAccountAndIdfa(resource.getAccountId(), resource.getIdfa(), resource.getPlatform());

        List<DeviceWhiteList> byAccountAndAndroidId = deviceWhiteListRepository.findByAccountAndAndroidId(resource.getAccountId(), resource.getAndroidId(), resource.getPlatform());
        List<DeviceWhiteList> byAccountAndImei = deviceWhiteListRepository.findByAccountAndImei(resource.getAccountId(), resource.getImei(), resource.getPlatform());
        List<DeviceWhiteList> byAccountAndOaid = deviceWhiteListRepository.findByAccountAndOaid(resource.getAccountId(), resource.getOaid(), resource.getPlatform());

        if (!isUpdate) {
            if (byAccountAndName.size() > 0) {
                if(!StringUtil.isEmpty(resource.getDeviceName())){
                    hashMap.put("deviceName", true);
                }
            }

            if (byAccountAndIdfa.size() > 0) {
                hashMap.put("idfa", true);
            }

            if (byAccountAndImei.size() > 0) {
                if(!StringUtil.isEmpty(resource.getImei())){
                    hashMap.put("imei", true);
                }
            }

            if (byAccountAndOaid.size() > 0) {
                if(!StringUtil.isEmpty(resource.getOaid())){
                    hashMap.put("oaid", true);
                }
            }

            if (byAccountAndAndroidId.size() > 0) {
                if(!StringUtil.isEmpty(resource.getAndroidId())){
                    hashMap.put("androidId", true);
                }
            }

        }else {
            if (byAccountAndName.size() == 1) {
                if(!byAccountAndName.get(0).getId().equals(resource.getId())){
                    hashMap.put("deviceName", true);
                }
            }
            if (byAccountAndIdfa.size() == 1) {
                if(!byAccountAndIdfa.get(0).getId().equals(resource.getId())){
                    hashMap.put("idfa", true);
                }
            }

            if (byAccountAndImei.size() > 0) {
                if(!StringUtil.isEmpty(resource.getImei())&&!byAccountAndImei.get(0).getId().equals(resource.getId())){
                    hashMap.put("imei", true);
                }
            }
            if (byAccountAndOaid.size() > 0) {
                if(!StringUtil.isEmpty(resource.getOaid())&&!byAccountAndOaid.get(0).getId().equals(resource.getId())){
                    hashMap.put("oaid", true);
                }
            }

            if (byAccountAndAndroidId.size() > 0) {
                if(!StringUtil.isEmpty(resource.getAndroidId())&&!byAccountAndAndroidId.get(0).getId().equals(resource.getId())){
                    hashMap.put("androidId", true);
                }
            }
        }
        return hashMap;
    }

    @Override
    public Object update(Account loginAccount,DeviceWhiteList resource) {
        HashMap<String, Boolean> valid = valid(loginAccount, resource, true);
        if(valid.size()>0){
            return valid;
        }
        return deviceWhiteListRepository.save(resource);
    }
}