package com.realnet.datasimulator.Services;
import com.realnet.datasimulator.Repository.BaddataRepository;
import com.realnet.datasimulator.Entity.Baddata
;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 org.springframework.stereotype.Service;

@Service
 public class BaddataService {
@Autowired
private BaddataRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;@Autowired
	private CountryService projectidserv;











public Baddata Savedata(Baddata data) {

 
if (data.getProjectid() != null) {
		try {	
  int projectidId = Integer.valueOf(data.getProjectid());
Country get =  projectidserv.getdetailsbyId(projectidId);
			if (get != null) {

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

		}

 












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


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


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


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


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

old.setDevicecategoryid(data.getDevicecategoryid());




old.setDeviceid(data.getDeviceid());




old.setPayload(data.getPayload());

old.setMappedpayload(data.getMappedpayload());

old.setUrl(data.getUrl());

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












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

	}}
