package com.realnet.stepperworkflow.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.realm.Entity.Realm;
import com.realnet.realm.Services.RealmService;
import com.realnet.stepperworkflow.Entity.Stepper_table_config;
import com.realnet.stepperworkflow.Repository.Stepper_table_configRepository;
import com.realnet.users.entity1.AppUser;
import com.realnet.users.service1.AppUserServiceImpl;

@Service
public class Stepper_table_configService {
	@Autowired
	private Stepper_table_configRepository Repository;
	@Autowired
	private AppUserServiceImpl userService;
	@Autowired
	private RealmService realmService;

	public Stepper_table_config Savedata(Integer stepperId, Integer tableId, String tableName) {

		Stepper_table_config data = new Stepper_table_config();

		data.setStepper_id(stepperId);
		data.setTable_id(tableId);
		data.setTable_name(tableName);

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

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

	public List<Stepper_table_config> getdetails() {
		List<Stepper_table_config> all = Repository.findAll(getUser().getUserId());

		return all;
	}

//	get ALl By table Id and stepper Id
	public List<Stepper_table_config> getAll(Integer stepper_id, Integer table_id) {
		List<Stepper_table_config> all = Repository.getAllBysteAndTbId(stepper_id, table_id);

		return all;
	}

//	get ALl By table Id and stepper Id
	public List<Stepper_table_config> getAll(Integer stepper_id) {
		List<Stepper_table_config> all = Repository.getAllBystepId(stepper_id);

		return all;
	}

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

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

	public Stepper_table_config update(Stepper_table_config data, Integer id) {
		Stepper_table_config old = Repository.findById(id).get();
		old.setTable_name(data.getTable_name());

		old.setTable_id(data.getTable_id());

		old.setStepper_id(data.getStepper_id());

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

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

	}
}
