/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sikd.ikonsol.ctrl;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.PrimeFaces;
import sikd.ikonsol.ejb.session.UserEjbHome;
import sikd.ikonsol.model.User;
import sikd.sikd5.sikd5bean.util.Crypto;

/**
 *
 * @author Imam Bhoim
 */
@ViewScoped
@Named(value = "userBeanCtrl")
public class UserBeanCtrl implements Serializable {

    User user;
    User obj;
    private List<User> list;

    private String username;
    private String password;
    private int status;

    @EJB
    UserEjbHome userEjbHome;

    @PostConstruct
    public void init() {

        list = userEjbHome.findAll();

        //obj = periodeEjbHome.findByKode("22");
        //periode = periodeEjbHome.createNew();
    }

    public void add() {

        user = userEjbHome.createNew();

    }
//

    public String save() {

        try {
            String inputString = user.getPasstext();

            byte[] p = new byte[inputString.trim().length()];
            for (int i = 0; i < p.length; i++) {
                p[i] = (byte) inputString.trim().charAt(i);
            }
            byte[] epass = Crypto.encrypt(p);

            // byte[] byteArrray = inputString.getBytes();
            user.setPassword(epass);

            if (user.getIdaktif() == 1) {
                user.setActives(true);
            } else {
                user.setActives(false);
            }

            userEjbHome.save(user);
        } catch (Exception e) {
            e.printStackTrace();

        }

        PrimeFaces.current().executeScript("PF('wg_entryform').hide()");
        PrimeFaces.current().ajax().update(":master:dtuser");

        list = userEjbHome.findAll();
        //this.list = service.findAll();
        return "index?faces-redirect=true";
    }

    public String update() {

        try {
            String inputString = obj.getPasstext();

            byte[] p = new byte[inputString.trim().length()];
            for (int i = 0; i < p.length; i++) {
                p[i] = (byte) inputString.trim().charAt(i);
            }
            byte[] epass = Crypto.encrypt(p);

            // byte[] byteArrray = inputString.getBytes();
            obj.setPassword(epass);
            
            System.out.println(epass);
            System.out.println("UPDATE");

            userEjbHome.update(obj);
        } catch (Exception e) {
            e.printStackTrace();

        }

        PrimeFaces.current().executeScript("PF('wg_entryform_ubah').hide()");
        PrimeFaces.current().ajax().update(":master:dtuser");

        list = userEjbHome.findAll();
        // this.list = service.findAll();
        return "index?faces-redirect=true";
    }

    public void edit(int id) {

        try {
            obj = userEjbHome.findByID(id);
            byte[] pass = Crypto.decrypt(obj.getPassword());

            String string = new String(pass);

            System.out.println(string);

            obj.setPasstext(string);

        } catch (Exception e) {
            e.printStackTrace();

        }

    }
//

    public void delete(int id) {
        //obj = service.findByID(Integer.parseInt(id));
        //obj = periodeEjbHome.findByKode(kode);
        userEjbHome.deleteId(id);
        PrimeFaces.current().ajax().update(":master:dtuser");
        list = userEjbHome.findAll();
        // this.list = service.findAll();

    }

    public User getUser() {
        return user;
    }

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

    public User getObj() {
        return obj;
    }

    public void setObj(User obj) {
        this.obj = obj;
    }

    public List<User> getList() {
        return list;
    }

    public void setList(List<User> list) {
        this.list = list;
    }

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

}
