<?php

/*
 * Copyright (c) 2018 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 *  contact@earworks.co.id
 */
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Description of User_lib
 *
 * @author Fajar Dwi Rianto <fajar@earworks.co.id>
 * @since 1.0
 */
class User_lib {

    public function login($input = array()) {
        $CI = & get_instance();
        $CI->load->helper('hash'); // required by user_model
        $user = $CI->user_model->get_login($input['username'], $input['password']);
        if (!$user) {
            return false;
        }
        $auth = sha1(CI_NAME . '_' . $user['userid'] . '_' . time());

        $user['userauth'] = $auth;
        $CI->user_model->update_auth($auth, $user['userid']);
        $CI->session->set_userdata(CI_NAME .'log', $CI->user_model->session_template($user));
        if ($input['remember'] == 'yes') {
            setcookie(CI_NAME . '_auth_key', $auth, strtotime("+30 days", time()), '/');
        }
        return true;
    }

    public function logout() {
        $CI = & get_instance();
        $user = $CI->session->userdata(CI_NAME .'log');
        $CI->session->unset_userdata(CI_NAME .'log');
        delete_cookie(CI_NAME . '_auth_key');
        $CI->user_model->update_auth(null, $user['userid']);
        $CI->session->sess_destroy();
    }

    public function change_password($input = array()) {
        $CI = & get_instance();
        $CI->load->helper('hash');
        $user = $CI->session->userdata(CI_NAME .'log');

        $validation = $CI->user_model->check_password($user['userid'], $input['old_password']);
        if (!$validation) {
            return false;
        }
        $CI->user_model->update_password($input['new_password'], $user['userid']);
        return true;
    }
    public function request_password($input = array()) {
        $CI = & get_instance();
        
        $user = $CI->user_model->get_email($input['email']);
        if(!$user){
            return false;
        }
        $code = sha1($user['userid'].CI_NAME).time();
        $CI->user_model->update_code($code, $user['userid']);
        $CI->load->helper('mail');
        return send_mail(array(
            'to' => $input['email'],
            'subject' => 'Password Reset',
            'message' => $CI->load->partials('email/forgot', array(
                'name' => $user['profilename'],
                'url' => base_url('backoffice/user/reset?code='.$code)
            ))
        ));
    }
    public function check_code($code){
        $CI = & get_instance();
        $timestamp = substr($code, -10, 10);
        if(strtotime("+30 minutes", $timestamp) < time()){
            return false;
        }
        $user = $CI->user_model->get_code($code);
        if(!$user) {
            return false;
        }
        return $user;
    }
    public function reset_password($input = array()) {
        $CI = & get_instance();
        $user = $CI->user_model->check_id($input['userid']);
        if(!$user){
            return false;
        }
        $CI->user_model->update_code(null, $input['userid']);
        $CI->user_model->update_password($input['new_password'], $input['userid']);
        return true;
    }
    public function change_profile($input = array()) {
        $CI = & get_instance();
        $CI->load->model('profile_model');
        $user = $CI->session->userdata(CI_NAME .'log');
        $profile = $CI->profile_model->check_user($user['userid']);
        if(!$profile){
            return false;
        }
        $input['profilepath'] = (!$input['profileupload']['error']) ? $input['profileupload']['data']['file_name'] : null;
        unset($input['profileupload']);
        $CI->profile_model->update($input, $user);
        $new = $CI->user_model->get_id($user['userid']);
        $CI->session->set_userdata('log', $CI->user_model->session_template($new));
        return true;
    }
    public function counter(){
        $CI = & get_instance();
        $CI->load->model('user_model');
        return $CI->user_model->get_counter();
    }
}
