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.Agent;
import com.realnet.vpspack.Entity.Gender;
import com.realnet.vpspack.Entity.Nationality;
import com.realnet.vpspack.Entity.Referrer;
import com.realnet.vpspack.Entity.Supplier;
import com.realnet.vpspack.Entity.Visa_application;
import com.realnet.vpspack.Entity.Visa_duration;
import com.realnet.vpspack.Entity.Visa_entry;
import com.realnet.vpspack.Entity.Visa_processing;
import com.realnet.vpspack.Repository.Visa_applicationRepository;

@Service
public class Visa_applicationService {
	@Autowired
	private Visa_applicationRepository Repository;
	@Autowired
	private AppUserServiceImpl userService;
	@Autowired
	private RealmService realmService;

	@Autowired
	private Visa_entryService visa_entry_typeserv;

	@Autowired
	private Visa_durationService visa_durationserv;

	@Autowired
	private Visa_processingService visa_processingserv;

	@Autowired
	private GenderService genderserv;

	@Autowired
	private ReferrerService referrerserv;

	@Autowired
	private NationalityService nationalityserv;

	@Autowired
	private SupplierService supplierserv;

	@Autowired
	private AgentService agentserv;

	public Visa_application Savedata(Visa_application data) {

		if (data.getVisa_entry_type() != null) {
			Visa_entry get = visa_entry_typeserv.getdetailsbyId(data.getVisa_entry_type());
			data.setVisa_entry_typename(get.getName());

		}

		if (data.getVisa_duration() != null) {
			Visa_duration get = visa_durationserv.getdetailsbyId(data.getVisa_duration());
			data.setVisa_durationname(get.getName());

		}

		if (data.getVisa_processing() != null) {
			Visa_processing get = visa_processingserv.getdetailsbyId(data.getVisa_processing());
			data.setVisa_processingname(get.getName());

		}

		if (data.getGender() != null) {
			Gender get = genderserv.getdetailsbyId(data.getGender());
			data.setGendername(get.getName());

		}

		if (data.getReferrer() != null) {
			Referrer get = referrerserv.getdetailsbyId(data.getReferrer());
			data.setReferrername(get.getName());

		}

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

		}

		if (data.getSupplier() != null) {
			Supplier get = supplierserv.getdetailsbyId(data.getSupplier());
			data.setSuppliername(get.getName());

		}

		if (data.getAgent() != null) {
			Agent get = agentserv.getdetailsbyId(data.getAgent());
			data.setAgentname(get.getName());

		}

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

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

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

		return all;
	}

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

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

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

		old.setDescription(data.getDescription());

		old.setActive(data.isActive());

		old.setVisa_entry_type(data.getVisa_entry_type());

		old.setVisa_duration(data.getVisa_duration());

		old.setVisa_processing(data.getVisa_processing());

		old.setTravel_start_date(data.getTravel_start_date());

		old.setTravel_end_date(data.getTravel_end_date());

		old.setPassport_number(data.getPassport_number());

		old.setPassport_issue_date(data.getPassport_issue_date());

		old.setPassport_expiry_date(data.getPassport_expiry_date());

		old.setEmail(data.getEmail());

		old.setPhone_number(data.getPhone_number());

		old.setBirth_place(data.getBirth_place());

		old.setDate_of_birth(data.getDate_of_birth());

		old.setGender(data.getGender());

		old.setProfession(data.getProfession());

		old.setVisa_cost(data.getVisa_cost());

		old.setReferrer(data.getReferrer());

		old.setNationality(data.getNationality());

		old.setSupplier(data.getSupplier());

		old.setAgent(data.getAgent());

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

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

	}
}
