package com.realnet.angulardatatype.Services;
import com.realnet.angulardatatype.Repository.Adv3Repository;
import com.realnet.angulardatatype.Entity.Adv3
;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.angulardatatype.Entity.Testlist;
import com.realnet.angulardatatype.Services.TestlistService;





import com.realnet.angulardatatype.Entity.Testlist;
import com.realnet.angulardatatype.Services.TestlistService;





	import org.springframework.stereotype.Service;

@Service
 public class Adv3Service {
@Autowired
private Adv3Repository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;





@Autowired
	private TestlistService dy2serv;



@Autowired
	private TestlistService autoserv;



public Adv3 Savedata(Adv3 data) {







 
if (data.getDy2() != null) {
		try {	
  int dy2Id = Integer.valueOf(data.getDy2());
Testlist get =  dy2serv.getdetailsbyId(dy2Id);
			if (get != null) {

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

		}

 





 
if (data.getAuto() != null) {
		try {	
  int autoId = Integer.valueOf(data.getAuto());
Testlist get =  autoserv.getdetailsbyId(autoId);
			if (get != null) {

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

			}

		}

 




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


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


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


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


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


old.setCountry(data.getCountry());

old.setStmlit(data.getStmlit());

old.setDy2(data.getDy2());

old.setDy1mul(data.getDy1mul());

old.setAuto(data.getAuto());

old.setAutomul(data.getAutomul());

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














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

	}}
