package controlers;

import beans.Korisnik;
import beans.Telefon;
import podaci.DB;
import java.util.LinkedList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

@ManagedBean(name = "controlerBean")
@SessionScoped
public class Controler {

    private String poruka;
    private String username;
    private String password;
    private Korisnik korisnik;
    private Telefon izabraniTelefon;
    private Telefon noviTelefon = new Telefon();
    private List<SelectItem> mobItems = new LinkedList<SelectItem>();

    public Controler() {
        mobItems.add(new SelectItem(true, "DA"));
        mobItems.add(new SelectItem(false, "NE"));
    }

    public String login() {
        String sledecaStranica = null;
        if (DB.isKorisnik(username, password)) {
            korisnik = DB.findKorisnikByUsername(username);
            korisnik.setTelefoni(DB.getTelefoniByUsername(username));
            sledecaStranica = "sviTelefoni";
            poruka = "";
        } else {
            poruka = "Ne postoji traženi korisnik u sistemu!";
        }
        return sledecaStranica;
    }

    public String logOut() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        session.invalidate();
        return "index";
    }

    public String detalji(Telefon tel) {
        this.izabraniTelefon = tel;
        return "detalji";
    }

    public void dodajTelefon() {
        this.korisnik.getTelefoni().add(noviTelefon);
        this.noviTelefon = new Telefon();
    }

    public void brisi() {
        this.korisnik.getTelefoni().remove(izabraniTelefon);
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Korisnik getKorisnik() {
        return korisnik;
    }

    public void setKorisnik(Korisnik korisnik) {
        this.korisnik = korisnik;
    }

    public Telefon getIzabraniTelefon() {
        return izabraniTelefon;
    }

    public void setIzabraniTelefon(Telefon izabraniTelefon) {
        this.izabraniTelefon = izabraniTelefon;
    }

    public Telefon getNoviTelefon() {
        return noviTelefon;
    }

    public void setNoviTelefon(Telefon noviTelefon) {
        this.noviTelefon = noviTelefon;
    }

    public List<SelectItem> getMobItems() {
        return mobItems;
    }

    public void setMobItems(List<SelectItem> mobItems) {
        this.mobItems = mobItems;
    }

    public String getPoruka() {
        return poruka;
    }

    public void setPoruka(String poruka) {
        this.poruka = poruka;
    }

}
