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.config.EmailService;










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 Adv3Service {
@Autowired
private Adv3Repository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;



@Autowired
	private EmailService emailServicedynamic;









@Autowired
	private CountryService dy2serv;

@Autowired
	private CountryService dy1serv;





public Adv3 Savedata(Adv3 data) {





 

try 
		   {

		// emailServicedynamic.sendEmail( "gaurav_dekatc_com","Adv3", "testing");
		emailServicedynamic.sendEmailViaSetu( "gaurav_dekatc_com","testing","","ganesh");

} catch (Exception e) {
			// TODO: handle exception
			System.out.println("Got error  During Mail Send " + e);
		}

 











 
if (data.getDy2() != null) {
		try {	
  int dy2Id = Integer.valueOf(data.getDy2());
Country 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.getDy1() != null) {
		try {	
  int dy1Id = Integer.valueOf(data.getDy1());
Country get =  dy1serv.getdetailsbyId(dy1Id);
			if (get != null) {

			data.setDy1identifier(get.getDescription());
}
} catch (NumberFormatException e) {
				System.out.println(" dy1Id is not integer..");
				// Invalid integer string — ignore or log
data.setDy1identifier(data.getDy1());
			}

		}

 






	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.setState(data.getState());

old.setStmlit(data.getStmlit());

old.setStmmlt2(data.getStmmlt2());

old.setDy2(data.getDy2());

old.setDy1(data.getDy1());

old.setDymlti1(data.getDymlti1());

old.setDymlt2(data.getDymlt2());

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






















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

	}}
