package com.realnet.basicp1.Services;
import com.realnet.basicp1.Repository.Adv2sRepository;
import com.realnet.basicp1.Entity.Adv2s
;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.Contry;
import com.realnet.basicp1.Services.ContryService;





import com.realnet.basicp1.Entity.Contry;
import com.realnet.basicp1.Services.ContryService;





import com.realnet.config.EmailService;


	import org.springframework.stereotype.Service;

@Service
 public class Adv2sService {
@Autowired
private Adv2sRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;





@Autowired
	private ContryService dyan_selserv;



@Autowired
	private ContryService autocserv;



@Autowired
	private EmailService emailServicedynamic;

public Adv2s Savedata(Adv2s data) {







 
if (data.getDyan_sel() != null) {
		try {	
  int dyan_selId = Integer.valueOf(data.getDyan_sel());
Contry get =  dyan_selserv.getdetailsbyId(dyan_selId);
			if (get != null) {

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

		}

 





 
if (data.getAutoc() != null) {
		try {	
  int autocId = Integer.valueOf(data.getAutoc());
Contry get =  autocserv.getdetailsbyId(autocId);
			if (get != null) {

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

			}

		}

 





 

try 
		   {

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

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

 


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


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


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


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


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

old.setSta_select(data.getSta_select());

old.setStat_mulsel(data.getStat_mulsel());

old.setDyan_sel(data.getDyan_sel());

old.setDyna_mul(data.getDyna_mul());

old.setAutoc(data.getAutoc());

old.setAuto_mul(data.getAuto_mul());



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
















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

	}}
