package com.uas.account.service;

import com.uas.account.dao.UserDao;
import com.uas.account.dao.UserQuestionDao;
import com.uas.account.dao.UserSpaceDao;
import com.uas.account.entity.UserDetail;
import com.uas.account.entity.UserQuestion;
import com.uas.account.entity.UserSpaceDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static org.apache.zookeeper.ZooDefs.OpCode.create;

/**
 * Created by wangmh on 2017/10/31.
 */
@Service
public class UserQuestionServiceImpl implements UserQuestionService {

    @Autowired
    private UserQuestionDao userQuestionDao;

    @Autowired
    private UserSpaceDao userSpaceDao;

    @Override
    public List<UserQuestion> getEncryptedQuestion(Long userUU) {
        return userQuestionDao.getEncryptedQuestion(userUU);
    }

    @Override
    public boolean checkEncryptedQuestion() {
        return false;
    }

    @Override
    public void save(List<UserQuestion> questions) {
        userQuestionDao.save(questions);
    }

    @Override
    public void saveOne(UserQuestion question) {
        userQuestionDao.saveOne(question);
    }

    @Override
    public List<UserQuestion> getDefaultQuestion(final UserDetail detail, int size) {
//        Class<? extends UserDetail> detailClass = detail.getClass();
//        Field[] declaredFields = detailClass.getDeclaredFields();
//        List<UserQuestion> questions = new ArrayList<>(size);
//        if (declaredFields.length < size) {
//            size = declaredFields.length;
//        }
//        // 记录被使用的字段下标，避免重复
//        List<Integer> usedIndex = new ArrayList<>(size);
//        Random random = new Random();
//        for (int i=0; i<size; i++) {
//            int index = random.nextInt(declaredFields.length);
//            Field field = declaredFields[index];
//            System.out.println(field.getName());
//        }
        List<UserQuestion> questions = new ArrayList<>(2);
        UserQuestion question = new UserQuestion();
        question.setQuestion("你注册的账户的姓名是什么？");
        Random random = new Random();
        question.setAnswer(detail.getName());
        question.setSort((short) 1);
        questions.add(question);
        question.setId(-1L);
        question = new UserQuestion();

        String tel = detail.getMobile();
        List<String> tels = new ArrayList<String>();
        tels.add(tel);
        List<UserSpaceDetailInfo> spaceDetailInfos = userSpaceDao.findByTel(tels);
        for(int i=0; i<spaceDetailInfos.size(); i++) {
            int index = random.nextInt(spaceDetailInfos.size());
            UserSpaceDetailInfo info = spaceDetailInfos.get(index);
            if (info.getCompany() == null) {
                continue;
            }
            if (info.getAddress() != null) {
                question.setQuestion("公司\""+info.getCompany()+"\"的地址是什么？");
                question.setAnswer(info.getAddress());
                questions.add(question);
                question.setSort((short) 2);
                question.setId(-2L);
                break;
            }
            if (info.getUsername() != null) {
                question.setQuestion("公司\""+info.getCompany()+"\"的管理员姓名是什么？");
                question.setAnswer(info.getUsername());
                questions.add(question);
                question.setSort((short) 2);
                question.setId(-2L);
                break;
            }
        }
        return questions;
    }
}
