<?php
defined('ABSPATH') || exit;
if (!class_exists('yascode_watchlist')) :
    class yascode_watchlist
    {
        //init before run
        private static $instance;
        public static function get_instance()
        {
            if (!self::$instance) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        public function __construct()
        {
            add_action('wp_ajax_yascode_add_watchlist_post', array($this, 'yascode_add_watchlist_post'));
            add_action('wp_ajax_yascode_panel_watchlist_toggle', array($this, 'yascode_panel_watchlist_toggle'));
        }

        public function yascode_panel_watchlist_toggle()
        {
            $result = array('success' => false);
            $post_id = (!empty($_POST['post_id']) && is_numeric($_POST['post_id'])) ? $_POST['post_id'] : null;
            $type = (!empty($_POST['type']) && ($_POST['type'] == 'add' || $_POST['type'] == 'remove')) ? $_POST['type'] : null;
            $post_type = get_post_type($post_id);
            if (!empty($post_id) && ($post_type == 'movies' || $post_type == 'series') && !empty($type)) {
                $user_id = get_current_user_id();
                $user_watchlist = get_user_meta($user_id, 'user_watchlist', true);
                $user_watchlist = (!empty($user_watchlist)) ? $user_watchlist : array();
                if ($type == 'remove') {
                    if (in_array($post_id, $user_watchlist)) {
                        $key = array_search($post_id, $user_watchlist);
                        unset($user_watchlist[$key]);
                        $result['msg'] = 'از علاقه مندی ها حذف شد !';
                        $result['result'] = 'done';
                    } else $result['msg'] = 'شناسه یافت نشد !';
                } else if ($type == 'add') {
                    $user_watchlist[] = $post_id;
                    $result['msg'] = 'به علاقه مندی ها اضافه شد';
                    $result['result'] = 'done';
                }
                update_user_meta($user_id, 'user_watchlist', $user_watchlist);
                $result['success'] = true;
            } else $result['msg'] = 'وضعیت نامعتبر';
            wp_send_json($result);
            wp_die();
        }

        public function yascode_add_watchlist_post()
        {
            $result = array('success' => false);
            $post_id = (!empty($_POST['post_id']) && is_numeric($_POST['post_id'])) ? $_POST['post_id'] : null;
            $post_type = get_post_type($post_id);
            if (!empty($post_id) && ($post_type == 'movies' || $post_type == 'series')) {
                $user_id = get_current_user_id();
                $user_watchlist = get_user_meta($user_id, 'user_watchlist', true);
                $user_watchlist = (!empty($user_watchlist)) ? $user_watchlist : array();
                if (in_array($post_id, $user_watchlist)) {
                    $key = array_search($post_id, $user_watchlist);
                    unset($user_watchlist[$key]);
                    $result['msg'] = 'از علاقه مندی ها حذف شد !';
                    $result['act'] = 'remove';
                } else {
                    $user_watchlist[] = $post_id;
                    $result['msg'] = 'به علاقه مندی ها اضافه شد';
                    $result['act'] = 'add';
                }
                update_user_meta($user_id, 'user_watchlist', $user_watchlist);
                $result['success'] = true;
            } else $result['msg'] = 'وضعیت نامعتبر';
            wp_send_json($result);
            wp_die();
        }

        public static function is_post_in_watchlist($post_id)
        {
            $result = false;
            if (is_user_logged_in()) {
                $user_id = get_current_user_id();
                $user_watchlist = get_user_meta($user_id, 'user_watchlist', true);
                $user_watchlist = (!empty($user_watchlist) && is_array($user_watchlist)) ? $user_watchlist : array();
                if (in_array($post_id, $user_watchlist)) $result = true;
            }
            return $result;
        }

        public static function get_user_count_watchlist($user_id)
        {
            $result = 0;
            $user_watchlist = get_user_meta($user_id, 'user_watchlist', true);
            $result = (!empty($user_watchlist) && is_array($user_watchlist)) ? count($user_watchlist) : 0;
            return $result;
        }

        public static function get_user_watchlist($user_id, $per_page = 10, $page = 1)
        {
            $result = array();
            $user_watchlist = get_user_meta($user_id, 'user_watchlist', true);
            $user_watchlist = (!empty($user_watchlist)) ? $user_watchlist : array();
            $total = count($user_watchlist);
            $totalPages = ceil($total / $per_page);
            $page = max($page, 1);
            $page = min($page, $totalPages);
            $offset = ($page - 1) * $per_page;
            if ($offset < 0) $offset = 0;
            $user_watchlist = array_slice($user_watchlist, $offset, $per_page);
            $result['items'] = $user_watchlist;
            $result['total_pages'] = $totalPages;
            return $result;
        }
    }
    yascode_watchlist::get_instance();
endif;
