package com.realnet.basicp1.Services;
import com.realnet.basicp1.Repository.Advtest2Repository;
import com.realnet.basicp1.Entity.Advtest2
;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.basicp1.Entity.Listtest;
import com.realnet.basicp1.Services.ListtestService;





import com.realnet.basicp1.Entity.Listtest;
import com.realnet.basicp1.Services.ListtestService;





	import org.springframework.stereotype.Service;

@Service
 public class Advtest2Service {
@Autowired
private Advtest2Repository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;



@Autowired
	private ListtestService dynamiserv;



@Autowired
	private ListtestService autocompleteserv;



public Advtest2 Savedata(Advtest2 data) {





 
if (data.getDynami() != null) {
		try {	
  int dynamiId = Integer.valueOf(data.getDynami());
Listtest get =  dynamiserv.getdetailsbyId(dynamiId);
			if (get != null) {

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

		}

 





 
if (data.getAutocomplete() != null) {
		try {	
  int autocompleteId = Integer.valueOf(data.getAutocomplete());
Listtest get =  autocompleteserv.getdetailsbyId(autocompleteId);
			if (get != null) {

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

		}

 




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


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


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


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


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

old.setStaticmul(data.getStaticmul());

old.setDynami(data.getDynami());

old.setDynamic_mul(data.getDynamic_mul());

old.setAutocomplete(data.getAutocomplete());

old.setAuto_mul(data.getAuto_mul());

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












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

	}}
