package com.realnet.basic1.Services;
import com.realnet.basic1.Repository.TestbugRepository;
import com.realnet.basic1.Entity.Testbug
;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.basic1.Entity.Testlist;
import com.realnet.basic1.Services.TestlistService;





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





	import org.springframework.stereotype.Service;

@Service
 public class TestbugService {
@Autowired
private TestbugRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;











@Autowired
	private TestlistService select_dyanamicserv;



@Autowired
	private TestlistService select_autocompserv;



public Testbug Savedata(Testbug data) {













 
if (data.getSelect_dyanamic() != null) {
		try {	
  int select_dyanamicId = Integer.valueOf(data.getSelect_dyanamic());
Testlist get =  select_dyanamicserv.getdetailsbyId(select_dyanamicId);
			if (get != null) {

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

		}

 





 
if (data.getSelect_autocomp() != null) {
		try {	
  int select_autocompId = Integer.valueOf(data.getSelect_autocomp());
Testlist get =  select_autocompserv.getdetailsbyId(select_autocompId);
			if (get != null) {

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

			}

		}

 




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


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


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


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


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

 


 
old.setBar_code(data.getBar_code());
 

 
old.setQr_code(data.getQr_code());
 

  

old.setChild(data.getChild());





old.setSelect_dyanamic(data.getSelect_dyanamic());

old.setSelect_dynamic_mul(data.getSelect_dynamic_mul());

old.setSelect_autocomp(data.getSelect_autocomp());

old.setSelect_autcomp_mul(data.getSelect_autcomp_mul());

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




















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

	}}
