package com.realnet.taxi_rental.Services;
import com.realnet.taxi_rental.Repository.RidesRepository;
import com.realnet.taxi_rental.Entity.Rides
;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.taxi_rental.Entity.Office;
import com.realnet.taxi_rental.Services.OfficeService;





import com.realnet.taxi_rental.Entity.Office;
import com.realnet.taxi_rental.Services.OfficeService;



import com.realnet.taxi_rental.Entity.Products;
import com.realnet.taxi_rental.Services.ProductsService;







	import org.springframework.stereotype.Service;

@Service
 public class RidesService {
@Autowired
private RidesRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;





@Autowired
	private OfficeService pick_upserv;



@Autowired
	private OfficeService return_addressserv;

@Autowired
	private ProductsService productserv;





public Rides Savedata(Rides data) {







 
if (data.getPick_up() != null) {
		try {	
  int pick_upId = Integer.valueOf(data.getPick_up());
Office get =  pick_upserv.getdetailsbyId(pick_upId);
			if (get != null) {

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

		}

 





 
if (data.getReturn_address() != null) {
		try {	
  int return_addressId = Integer.valueOf(data.getReturn_address());
Office get =  return_addressserv.getdetailsbyId(return_addressId);
			if (get != null) {

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

		}

 



 
if (data.getProduct() != null) {
		try {	
  int productId = Integer.valueOf(data.getProduct());
Products get =  productserv.getdetailsbyId(productId);
			if (get != null) {

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

		}

 






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


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


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


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


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

old.setDescription(data.getDescription());

old.setActive (data.getActive());

old.setPick_up(data.getPick_up());

old.setReturn_to_same_location (data.getReturn_to_same_location());

old.setReturn_address(data.getReturn_address());

old.setProduct(data.getProduct());

old.setVehicle_add_on(data.getVehicle_add_on());

old.setIscompleted (data.getIscompleted());

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


















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

	}}
