package com.realnet.vpspack.Services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.realnet.realm.Entity.Realm;
import com.realnet.realm.Services.RealmService;
import com.realnet.users.entity1.AppUser;
import com.realnet.users.service1.AppUserServiceImpl;
import com.realnet.vpspack.Entity.Nationality;
import com.realnet.vpspack.Entity.Visa_administration;
import com.realnet.vpspack.Entity.Visa_status;
import com.realnet.vpspack.Repository.Visa_administrationRepository;

@Service
public class Visa_administrationService {
	@Autowired
	private Visa_administrationRepository Repository;
	@Autowired
	private AppUserServiceImpl userService;
	@Autowired
	private RealmService realmService;

	@Autowired
	private Visa_statusService visa_statusserv;

	@Autowired
	private NationalityService nationalityserv;

	public Visa_administration Savedata(Visa_administration data) {

		if (data.getVisa_status() != null) {
			Visa_status get = visa_statusserv.getdetailsbyId(data.getVisa_status());
			data.setVisa_statusname(get.getName());

		}

		if (data.getNationality() != null) {
			Nationality get = nationalityserv.getdetailsbyId(data.getNationality());
			data.setNationalityname(get.getName());

		}

		data.setUpdatedBy(getUser().getUserId());
		data.setCreatedBy(getUser().getUserId());
		data.setAccountId(getUser().getAccount().getAccount_id());
		Visa_administration save = Repository.save(data);
		return save;
	}

//	get all with pagination
	public Page<Visa_administration> getAllWithPagination(Pageable page) {
		return Repository.findAll(page, getUser().getUserId());
	}

	public List<Visa_administration> getdetails() {
		List<Realm> realm = realmService.findByUserId(getUser().getUserId());
		List<Visa_administration> all = Repository.findAll(getUser().getUserId());

		return all;
	}

	public Visa_administration getdetailsbyId(Integer id) {
		return Repository.findById(id).get();
	}

	public void delete_by_id(Integer id) {
		Repository.deleteById(id);
	}

	public Visa_administration update(Visa_administration data, Integer id) {
		Visa_administration old = Repository.findById(id).get();
		old.setName(data.getName());

		old.setDescription(data.getDescription());

		old.setActive(data.isActive());

		old.setEmails(data.getEmails());

		old.setVisa_status(data.getVisa_status());

		old.setNationality(data.getNationality());

		final Visa_administration test = Repository.save(old);
		data.setUpdatedBy(getUser().getUserId());
		return test;
	}

	public AppUser getUser() {
		AppUser user = userService.getLoggedInUser();
		return user;

	}
}
