/*
 * 文件名:MasterServiceImpl.java
 * 版权:Copyright by www.cheer.com
 * 描述:
 * 修改人:abc
 * 修改时间:2017年11月6日
 */


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cheer.hibernate.adopt.Master;
import com.cheer.hibernate.adopt.Pet;
import com.cheer.hibernate.adopt.PetType;
import com.cheer.hibernate.dao.MasterDao;
import com.cheer.hibernate.dao.PetDao;
import com.cheer.hibernate.dao.PetTypeDao;
import com.cheer.hibernate.dao.impl.MasterDaoImpl;
import com.cheer.hibernate.dao.impl.PetDaoImpl;
import com.cheer.hibernate.dao.impl.PetTypeDaoImpl;
import com.cheer.hibernate.service.MasterService;


public class MasterServiceImpl implements MasterService
{
    private static final Logger LOGGER = LogManager.getLogger(MasterServiceImpl.class);

    @Override
    public boolean login(Master master, Session session, Transaction tx)
    {
        MasterDao masterDao = new MasterDaoImpl();
        if (masterDao.findMaster(master, session, tx) == 0)
        {
            LOGGER.info("登陆失败");
            return false;
        }

        LOGGER.info("登陆成功");
        return true;
    }

    @Override
    public boolean adoptPet(Session session, Transaction tx)
    {
        Scanner sc = new Scanner(System.in);
        String username = null;
        String password = null;
        String select = null;
        String name = null;
        String typeId = null;
        PetType petType = null;

        LOGGER.info("----欢迎光临宠物乐园----");
        LOGGER.info("请输入登入名:");
        username = sc.next();
        LOGGER.info("请输入密码:");
        password = sc.next();
        Master master = new Master(username, password);
        if (!login(master, session, tx))
        {
            sc.close();
            return false;
        }

        LOGGER.info("请选择宠物类型:");
        PetTypeDao petTypeDao = new PetTypeDaoImpl();
        List<PetType> petTypes = petTypeDao.findAllPetType(session, tx);
        List<String> petTypeNames = new ArrayList<>();

        StringBuffer sb = new StringBuffer();
        for (PetType element : petTypes)
        {
            petTypeNames.add(element.getName());
            sb.append(element.getName()).append(" | ");
        }

        LOGGER.info(sb);

        select = sc.next();
        if (!petTypeNames.contains(select))
        {
            LOGGER.info("宠物类型输入有误,领养失败!");
            sc.close();
            return false;
        }

        for (PetType element : petTypes)
        {
            if (select.equals(element.getName()))
            {
                typeId = element.getId();
                petType = element;
                break;
            }
        }

        LOGGER.info("您选择的宠物类型编号是{}", typeId);
        LOGGER.info("请输入宠物昵称:");
        name = sc.next();

        sc.close();
        LOGGER.info(petType);
        Pet pet = new Pet(name, 88, 99, "2014-08-14", "健康", master, petType);
        PetDao petDao = new PetDaoImpl();

        petDao.save(pet, session, tx);

        return true;
    }
}