package com.realnet.angulardatatype.Services;
import com.realnet.angulardatatype.Repository.Ad8Repository;
import com.realnet.angulardatatype.Entity.Ad8
;import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import com.realnet.SequenceGenerator.Service.SequenceService;
import com.realnet.Notification.Entity.NotificationService;
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.angulardatatype.Entity.Support;

import com.realnet.angulardatatype.Repository.SupportRepository;


	import org.springframework.stereotype.Service;

@Service
 public class Ad8Service {
@Autowired
private Ad8Repository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;





public Ad8 Savedata(Ad8 data) {






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


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


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


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


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

old.setSupport(data.getSupport());



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





//	update Action

	@Autowired
	private SupportRepository supportupdaterepository;


	public ResponseEntity<?> updateSupport(Integer id, Support data) {
		Integer i = 0;
		Ad8 s = Repository.findById(id).get();

		List<Support> list = new ArrayList<>();

		Object supportObject = s.getSupport();

		if (supportObject instanceof List<?>) {
			// If it's a list, cast and add all elements to the list
			list.addAll((List<Support>) supportObject);
		} else if (supportObject instanceof Support) {
			// If it's a single Support object, add it to the list
			list.add((Support) supportObject);
		}
		

		for (Support li : list) {
			Support old = supportupdaterepository.findById(li.getId()).get();

		
			
			final Support supportdata = supportupdaterepository.save(old);
			i++;
		}
		return new ResponseEntity<>(i + " updated", HttpStatus.OK);
	}

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

	}}
