<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Entity;

use InvalidArgumentException;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Entity\Traits\EntityUpdater;
class Export
{
    use EntityUpdater;
    const POST_TYPE_SLUG = 'dropshipping_export';
    const STATUS_WAITING = 'waiting';
    const STATUS_IN_PROGRESS = 'in_progress';
    const STATUS_INIT = 'init';
    const STATUS_STOPPED = 'stopped';
    const STATUS_ERROR = 'error';
    /**
     * @var int
     */
    private $id = 0;
    /**
     * @var int
     */
    private $user_id = 0;
    /**
     * @var string
     */
    private $uid = '';
    /**
     * @var string
     */
    private $url = '';
    /**
     * @var int
     */
    private $start_date = 0;
    /**
     * @var int
     */
    private $end_date = 0;
    /**
     * @var int
     */
    private $next_export = 0;
    /**
     * @var int
     */
    private $last_position = 0;
    /**
     * @var int
     */
    private $products_count = 0;
    /**
     * @var string
     */
    private $export_type;
    /**
     * @var string
     */
    private $status = self::STATUS_WAITING;
    /**
     * @var int
     */
    private $created = 0;
    /**
     * @var int
     */
    private $skipped = 0;
    /**
     * @var string
     */
    private $date_created = '';
    /**
     * @var string
     */
    private $cron_schedule = '';
    /**
     * @var string
     */
    private $error_message = '';
    /**
     * @var string
     */
    private $export_name = '';
    public function get_user_id(): int
    {
        return $this->user_id;
    }
    public function set_user_id(int $user_id)
    {
        $this->user_id = $user_id;
    }
    public function get_next_export(): int
    {
        return $this->next_export;
    }
    public function set_next_export(int $next_import)
    {
        $this->next_export = $next_import;
    }
    public function get_start_date(): int
    {
        return $this->start_date;
    }
    public function set_start_date(int $start_date)
    {
        $this->start_date = $start_date;
    }
    public function get_end_date(): int
    {
        return $this->end_date;
    }
    public function set_end_date(int $end_date)
    {
        $this->end_date = $end_date;
    }
    public function get_id(): int
    {
        return $this->id;
    }
    public function set_id(int $id)
    {
        $this->id = $id;
    }
    public function get_uid(): string
    {
        return $this->uid;
    }
    public function set_uid(string $uid)
    {
        $this->uid = $uid;
    }
    public function get_url(): string
    {
        return $this->url;
    }
    public function set_url(string $url)
    {
        $this->url = $url;
    }
    public function get_last_position(): int
    {
        return $this->last_position;
    }
    public function set_last_position(int $last_position)
    {
        $this->last_position = $last_position;
    }
    public function get_status(): string
    {
        return $this->status;
    }
    public function set_status(string $status)
    {
        if (!in_array($status, $this->get_statuses())) {
            throw new InvalidArgumentException('Error, wrong status ' . $status . ' given');
        }
        $this->status = $status;
    }
    public function set_products_count(int $count)
    {
        $this->products_count = $count;
    }
    public function get_products_count(): int
    {
        return $this->products_count;
    }
    public function set_created(int $count)
    {
        $this->created = $count;
    }
    public function get_created(): int
    {
        return $this->created;
    }
    public function set_skipped(int $count)
    {
        $this->skipped = $count;
    }
    public function get_skipped(): int
    {
        return $this->skipped;
    }
    public function add_to_created(int $number)
    {
        $this->created += $number;
    }
    public function add_to_skipped(int $number)
    {
        $this->skipped += $number;
    }
    public function add_to_last_position(int $number)
    {
        $this->last_position += $number;
    }
    public function get_post_type(): string
    {
        return self::POST_TYPE_SLUG;
    }
    public function get_progress(): int
    {
        $result = 0;
        if (!empty($this->get_products_count())) {
            return intval($this->get_checked_products() / $this->get_products_count() * 100);
        }
        return $result;
    }
    public function get_formated_progress(): string
    {
        return strval(number_format($this->get_progress(), 0)) . '%';
    }
    public function get_checked_products(): int
    {
        return $this->get_created() + $this->get_skipped();
    }
    public function set_date_created(string $date)
    {
        $this->date_created = $date;
    }
    public function get_date_created(): string
    {
        return $this->date_created;
    }
    public function set_cron_schedule(string $schedule)
    {
        $this->cron_schedule = $schedule;
    }
    public function get_cron_schedule(): string
    {
        return $this->cron_schedule;
    }
    public function set_error_message(string $message)
    {
        $this->error_message = $message;
    }
    public function get_error_message(): string
    {
        return $this->error_message;
    }
    public function set_export_name(string $name)
    {
        $this->export_name = $name;
    }
    public function get_export_name(): string
    {
        return $this->export_name;
    }
    private function get_statuses(): array
    {
        return [self::STATUS_WAITING, self::STATUS_IN_PROGRESS, self::STATUS_INIT, self::STATUS_STOPPED, self::STATUS_ERROR];
    }
    public function set_export_type(string $export_type)
    {
        $this->export_type = $export_type;
    }
    public function get_export_type(): string
    {
        return $this->export_type;
    }
}
