package com.realnet.angulardatatype.Services;
import com.realnet.angulardatatype.Repository.Adv4Repository;
import com.realnet.angulardatatype.Entity.Adv4
;import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import com.realnet.SequenceGenerator.Service.SequenceService;
import org.springframework.data.domain.Page;
import com.realnet.realm.Entity.Realm;
import com.realnet.realm.Services.RealmService;
import org.springframework.data.domain.Pageable;
import org.springframework.http.*;
import com.realnet.users.service1.AppUserServiceImpl;
import com.realnet.users.entity1.AppUser;
import com.realnet.testdata.Entity.Country;
import com.realnet.testdata.Services.CountryService;



import com.realnet.testdata.Entity.Country;
import com.realnet.testdata.Services.CountryService;











	import org.springframework.stereotype.Service;

@Service
 public class Adv4Service {
@Autowired
private Adv4Repository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;@Autowired
	private CountryService atocserv;

@Autowired
	private CountryService atc2serv;









public Adv4 Savedata(Adv4 data) {

 
if (data.getAtoc() != null) {
		try {	
  int atocId = Integer.valueOf(data.getAtoc());
Country get =  atocserv.getdetailsbyId(atocId);
			if (get != null) {

			data.setAtocidentifier(get.getName());
}
} catch (NumberFormatException e) {
				System.out.println(" atocId is not integer..");
				// Invalid integer string — ignore or log
data.setAtocidentifier(data.getAtoc());

			}

		}

 



 
if (data.getAtc2() != null) {
		try {	
  int atc2Id = Integer.valueOf(data.getAtc2());
Country get =  atc2serv.getdetailsbyId(atc2Id);
			if (get != null) {

			data.setAtc2identifier(get.getName());
}
} catch (NumberFormatException e) {
				System.out.println(" atc2Id is not integer..");
				// Invalid integer string — ignore or log
data.setAtc2identifier(data.getAtc2());

			}

		}

 










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


//	get all with pagination
	public Page<Adv4> getAllWithPagination(Pageable page) {
		return Repository.findAll( getUser().getUserId(),page);
	}			
public List<Adv4> getdetails() {  
		List<Realm> realm = realmService.findByUserId(getUser().getUserId());
List<Adv4> all = Repository.findAll(getUser().getUserId());
		
		return all ;		}


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


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


public Adv4 update(Adv4 data,Integer id) {
	Adv4 old = Repository.findById(id).get();
old.setAtoc(data.getAtoc());

old.setAtc2(data.getAtc2());

old.setAtdy1(data.getAtdy1());

old.setAtdy2(data.getAtdy2());

old.setSupport(data.getSupport());

old.setChildform(data.getChildform());

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












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

	}}
