package com.realnet.basicp1.Services;
import com.realnet.basicp1.Repository.FormaRepository;
import com.realnet.basicp1.Entity.Forma
;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.Lista;
import com.realnet.basicp1.Services.ListaService;





import com.realnet.basicp1.Entity.Lista;
import com.realnet.basicp1.Services.ListaService;





	import org.springframework.stereotype.Service;

@Service
 public class FormaService {
@Autowired
private FormaRepository Repository;
	@Autowired
	private AppUserServiceImpl userService; 
@Autowired
	private RealmService realmService;



























@Autowired
 private SequenceService documentsequencesequenceService;







@Autowired
	private ListaService select_dyaserv;



@Autowired
	private ListaService select_autoserv;



public Forma Savedata(Forma data) {




























data.setDocumentsequence (documentsequencesequenceService.GenerateSequence("101"));

data.setUser_id(getUser().getUserId());
		data.setUser_name(getUser().getFullName());






 
if (data.getSelect_dya() != null) {
		try {	
  int select_dyaId = Integer.valueOf(data.getSelect_dya());
Lista get =  select_dyaserv.getdetailsbyId(select_dyaId);
			if (get != null) {

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

		}

 





 
if (data.getSelect_auto() != null) {
		try {	
  int select_autoId = Integer.valueOf(data.getSelect_auto());
Lista get =  select_autoserv.getdetailsbyId(select_autoId);
			if (get != null) {

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

		}

 




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


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


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


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


public Forma update(Forma data,Integer id) {
	Forma old = Repository.findById(id).get();
old.setNamess(data.getNamess());

old.setNumber_field(data.getNumber_field());

old.setPhone_number(data.getPhone_number());

old.setParagraph_field(data.getParagraph_field());

old.setPassword_field(data.getPassword_field());

old.setTextarea(data.getTextarea());

old.setDate_field(data.getDate_field());

old.setDatetime_field(data.getDatetime_field());

old.setEmail_field(data.getEmail_field());

old.setToggle_switch (data.getToggle_switch());

old.setUrl_field(data.getUrl_field());

old.setDecimal_field(data.getDecimal_field());

old.setPercentage_field(data.getPercentage_field());

old.setRecaptcha(data.getRecaptcha());

old.setDocumentsequence(data.getDocumentsequence());



old.setSelect_stat(data.getSelect_stat());

old.setSelect_mul(data.getSelect_mul());

old.setSelect_dya(data.getSelect_dya());

old.setSelect_dyamu(data.getSelect_dyamu());

old.setSelect_auto(data.getSelect_auto());

old.setSelect_automul(data.getSelect_automul());

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












































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

	}}
