package com.realnet.dlf.Services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.realnet.dlf.Entity.Dlf_header;
import com.realnet.dlf.Repository.Dlf_headerRepository;
import com.realnet.realm.Entity.Realm;
import com.realnet.realm.Services.RealmService;
import com.realnet.users.entity1.AppUser;
import com.realnet.users.service1.AppUserServiceImpl;

@Service
public class Dlf_headerService {
	@Autowired
	private Dlf_headerRepository Repository;
	@Autowired
	private AppUserServiceImpl userService;
	@Autowired
	private RealmService realmService;

	public Dlf_header Savedata(Dlf_header data) {

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

//	get all with pagination
	public Page<Dlf_header> getAllWithPagination(Pageable page) {
		return Repository.findAll(page, getUser().getUserId());
	}

	public List<Dlf_header> getdetails() {
		List<Realm> realm = realmService.findByUserId(getUser().getUserId());
		List<Dlf_header> all = Repository.findAll(getUser().getUserId());

		return all;
	}

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

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

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

		old.setDescription(data.getDescription());

		old.setActive(data.isActive());

		old.setType(data.getType());
		old.setTechstack(data.getTechstack());

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

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

	}
}
