<?php

defined('BASEPATH') or exit('No direct script access allowed');

class User_model extends CI_Model
{

    // var $id = '';
    var $login = '';
    var $password = '';
    var $full_name = '';
    var $phone = '';
    var $email = '';
    var $user_class = '';
    var $activated = true;
    var $modified_date = '';
    var $created_date = '';

    public function __construct()
    {
        parent::__construct();
    }

    public function count_all()
    {
        return $this->db->count_all('app_user');
    }

    public function count_row_for_query($query)
    {
        return $query->num_rows();
    }

    public function get_all_users()
    {
        $query = $this->db->get('app_user');
        return $query->result();
    }

    public function get_user_by_id($id)
    {
        $query = $this->db->get_where('app_user', array('id' => $id));
        $users = $query->result();
        $user = [];
        if (count($users) > 0) {
            foreach ($users as $row) {
                $user = $row;
            }
        }

        return $user;
    }

    public function get_user_by_email($id)
    {
        $query = $this->db->get_where('app_user', array('email' => $id));
        $users = $query->result();
        $user = [];
        if (count($users) > 0) {
            foreach ($users as $row) {
                $user = $row;
            }
        }

        return $user;
    }

    public function is_email_exists($id)
    {
        $query = $this->db->get_where('app_user', array('email' => $id));
        $users = $query->result();
        $user = [];
        if (count($users) > 0) {
            return true;
        }
        return false;
    }



    public function get_user_by_phone($id)
    {
        $query = $this->db->get_where('app_user', array('phone' => $id));
        $users = $query->result();
        $user = [];
        if (count($users) > 0) {
            foreach ($users as $row) {
                $user = $row;
            }
        }

        return $user;
    }

    public function is_phone_exists($id)
    {
        $query = $this->db->get_where('app_user', array('phone' => $id));
        $users = $query->result();
        $user = [];
        if (count($users) > 0) {
            return true;
        }
        return false;
    }



    public function get_user_by_username($username)
    {
        $query = $this->db->get_where('app_user', array('login' => $username));
        $users = $query->result();
        $user = [];
        if (count($users) > 0) {
            foreach ($users as $row) {
                $user = $row;
            }
        }

        return $user;
    }

    public function is_username_exists($id)
    {
        $query = $this->db->get_where('app_user', array('login' => $id));
        $users = $query->result();
        $user = [];
        if (count($users) > 0) {
            return true;
        }
        return false;
    }

    public function get_logged_in_user($username, $password)
    {
        $this->db->select('id, login, password, full_name, phone, email, created_date, modified_date, logged_in, last_login_date, user_class');
        $query = $this->db->get_where('app_user', array('login' => $username));
        $hashed_password = '';
        $user = [];
        foreach ($query->result() as $row) {
            $hashed_password = $row->password;
            $user = $row;
        }
        if (password_verify($password, $hashed_password)) {
            unset($user->password);
            if ($this->db->set('logged_in', true)
                ->where('id', $user->id)
                ->update('app_user')
            ) {
                return $user;
            }
        }
        return null;
    }

    public function logout($username)
    {
        $this->db->select('id, login, password, full_name, phone, email, created_date, modified_date, logged_in, last_login_date');
        $query = $this->db->get_where('app_user', array('login' => $username));
        $user = [];
        foreach ($query->result() as $row) {
            $user = $row;
        }
        if ($this->db->set('logged_in', false)
            ->where('id', $user->id)
            ->update('app_user')
        ) {
            return true;
        }
        return false;
    }

    public function insert_app_user()
    {
        $stream_clean = $this->security->xss_clean($this->input->raw_input_stream);
        $request = json_decode($stream_clean);
        $this->login = $request->login;
        $this->password = password_hash($request->password, PASSWORD_DEFAULT);
        $this->full_name = $request->full_name;
        $this->phone = $request->phone;
        $this->email = $request->email;
        $this->user_class = 'USER';
        $this->activated = true;
        $this->created_date = date('yyyy-MM-dd');

        $emailIsExist = $this->is_email_exists($this->email);
        $phoneIsExist = $this->is_phone_exists($this->phone);
        $loginIsExist = $this->is_username_exists($this->login);


        if ($emailIsExist) {
            $productArray = ['code' => '99', 'message' => 'Email Already Exist!', 'data' => ''];
            return $productArray;
        } elseif ($phoneIsExist) {
            $productArray = ['code' => '99', 'message' => 'Phone Number already exists!', 'data' => ''];
            return $productArray;
        } elseif ($loginIsExist) {
            $productArray = ['code' => '99', 'message' => 'Username already exists!', 'data' => ''];
            return $productArray;
        } else {

            if ($this->db->insert('app_user', $this)) {
                $productArray = ['code' => '00', 'message' => 'User Created Successfully!', 'data' => true];
                return $productArray;
            } else {
                $productArray = ['code' => '99', 'message' => 'Error in creating user!', 'data' => false];
                return $productArray;
            }
        }
    }

    public function insert_app_user_admin($login, $password, $full_name, $phone, $email, $type)
    {
        $this->login = $login;
        $this->password = password_hash($password, PASSWORD_DEFAULT);
        $this->full_name = $full_name;
        $this->phone = $phone;
        $this->email = $email;
        $this->user_class = $type;
        $this->activated = true;
        $this->created_date = date('yyyy-MM-dd');

        $emailIsExist = $this->is_email_exists($this->email);
        $phoneIsExist = $this->is_phone_exists($this->phone);
        $loginIsExist = $this->is_username_exists($this->login);


        if ($emailIsExist) {
            $productArray = ['code' => '99', 'message' => 'Email Already Exist!', 'data' => false];
            return $productArray;
        } elseif ($phoneIsExist) {
            $productArray = ['code' => '99', 'message' => 'Phone Number already exists!', 'data' => false];
            return $productArray;
        } elseif ($loginIsExist) {
            $productArray = ['code' => '99', 'message' => 'Username already exists!', 'data' => false];
            return $productArray;
        } else {

            if ($this->db->insert('app_user', $this)) {
                $productArray = ['code' => '00', 'message' => 'User Created Successfully!', 'data' => true];
                return $productArray;
            } else {
                $productArray = ['code' => '99', 'message' => 'Error in creating user!', 'data' => false];
                return $productArray;
            }
        }
    }

    public function update_app_user($array)
    {
        $query = $this->db->get_where('app_user', array('id' => $array['id']));
        $user = [];
        foreach ($query->result() as $row) {
          $user = $row;
        }
        
        if($_SESSION['username'] == $user->login){
            $_SESSION['username'] = $array['login'];
        }

        if ($this->db->set('login', $array['login'])
        ->set('password', password_hash($array['password'], PASSWORD_DEFAULT))
        ->set('email', $array['email'])
        ->set('phone', $array['phone'])
        ->set('full_name', $array['full_name'])
        ->set('user_class', $array['user_class'])
        ->where('id', $array['id'])
        ->update('app_user')
    ){
            $productArray = ['code' => '00', 'message' => 'User Updated Successfully!', 'data' => true];
            return $productArray;
        } else {
            $productArray = ['code' => '99', 'message' => 'Error in updating user!', 'data' => false];
            return $productArray;
        }
    }

    public function deactivate_user($id)
    {
        if ($this->db->set('logged_in', false)
            ->set('activated', 0)
            ->where('id', $id)
            ->update('app_user')
        ) {
            return true;
        }
        return false;
    }

    public function activate_user($id)
    {
        $this->db->select('id, login, password, full_name, phone, email, created_date, modified_date, logged_in, last_login_date');
        $query = $this->db->get_where('app_user', array('id' => $id));
        $user = [];
        foreach ($query->result() as $row) {
            $user = $row;
        }
        if ($this->db->set('activated', true)
            ->where('id', $user->id)
            ->update('app_user')
        ) {
            return true;
        }
        return false;
    }
}
