/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.triz.trizservice.bean;

import com.triz.trizservice.modeles.ServiceGroupe;
import com.triz.trizservice.modeles.ServiceIncrementation;
import com.triz.trizservice.modeles.ServiceMember;
import com.triz.trizservice.modeles.ServiceUser;
import com.triz.trizservice.modeles.ServicehistoriqueConnection;
import com.triz.trizservice.modeles.ServiceHistoriqueManipulation;
import com.triz.trizservice.service.TransactionService;
import com.triz.util.UtilContext;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 *
 * @author Asma
 */
@Component("formUserBean")
@Scope("view")
public class FormUserBean {

    private boolean canEdit;
    private boolean isNew;
    private String user1;
    private String modifier;
    private ServiceUser user;
    private ServiceUser userSelected;
    private ServiceIncrementation incrementationSelected;
    private List<ServicehistoriqueConnection> listeHistCnx = new ArrayList<>();
    private List<ServiceHistoriqueManipulation> listehistManipulation = new ArrayList<>();
    private List<ServiceGroupe> listeGroups;
    private ServiceMember member;

    @Autowired
    public TransactionService service;

    @Autowired
    public UtilContext context;

    @PostConstruct
    public void init() {
        System.out.println("//--------------init()FormUserBean ");
        user = context.getCurrentUser();
        listeGroups = service.getAllGroups();

        if (modifier != null) {
            if (modifier.equals("true")) {
                canEdit = true;
            }
        }
        System.out.println("Utilisateur" + user1);
        if (user1 != null) {
            userSelected = service.getUserById(user1);
            member = service.getGroupByUser(userSelected);
            System.out.println("Utilisateur" + userSelected);
            listeHistCnx = service.getListHistoriqueConnectionByUsers(userSelected);
            listehistManipulation = service.getListHistoriqueManipulationByUsersByEntreprise(userSelected);
        } else {
            if (canEdit) {
                isNew = true;
            }
            incrementationSelected = new ServiceIncrementation();
            userSelected = new ServiceUser();
            member = new ServiceMember();
        }
    }

    public ServiceGroupe getGroupeOfUser(ServiceUser user) {
        member = service.getGroupByUser(user);
        return member.getFkGroupe();
    }

    public String saveUser() {
        service.saveUser(userSelected);
        member.setFkUser(userSelected);
        service.saveMembership(member);
        return voirUser();
    }

    public String voirUser() {
        if (!isNew) {
            if (!canEdit) {
                return "list.xhtml?faces-redirect=true";
            } else {
                return "form.xhtml?faces-redirect=true&user1=" + userSelected.getUserid();
            }
        } else {
            return "list.xhtml?faces-redirect=true";
        }
    }

    public String editUser() {
        return "form.xhtml?faces-redirect=true&canEdit=true&user1=" + userSelected.getUserid();
    }

    public boolean isCanEdit() {
        return canEdit;
    }

    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
    }

    public boolean isIsNew() {
        return isNew;
    }

    public void setIsNew(boolean isNew) {
        this.isNew = isNew;
    }

    public String getUser1() {
        return user1;
    }

    public void setUser1(String user1) {
        this.user1 = user1;
    }

    public String getModifier() {
        return modifier;
    }

    public void setModifier(String modifier) {
        this.modifier = modifier;
    }

    public ServiceUser getUser() {
        return user;
    }

    public void setUser(ServiceUser user) {
        this.user = user;
    }

    public ServiceUser getUserSelected() {
        return userSelected;
    }

    public void setUserSelected(ServiceUser userSelected) {
        this.userSelected = userSelected;
    }

    public ServiceIncrementation getIncrementationSelected() {
        return incrementationSelected;
    }

    public void setIncrementationSelected(ServiceIncrementation incrementationSelected) {
        this.incrementationSelected = incrementationSelected;
    }

    public List<ServicehistoriqueConnection> getListeHistCnx() {
        return listeHistCnx;
    }

    public void setListeHistCnx(List<ServicehistoriqueConnection> listeHistCnx) {
        this.listeHistCnx = listeHistCnx;
    }

    public List<ServiceHistoriqueManipulation> getListehistManipulation() {
        return listehistManipulation;
    }

    public void setListehistManipulation(List<ServiceHistoriqueManipulation> listehistManipulation) {
        this.listehistManipulation = listehistManipulation;
    }

    public List<ServiceGroupe> getListeGroups() {
        return listeGroups;
    }

    public void setListeGroups(List<ServiceGroupe> listeGroups) {
        this.listeGroups = listeGroups;
    }

    public ServiceMember getMember() {
        return member;
    }

    public void setMember(ServiceMember member) {
        this.member = member;
    }
}
