<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if ( class_exists( 'WPDesk_Flexible_Shipping_Manifest' ) ) {

    class WPDesk_Flexible_Shipping_Manifest_paczkomaty extends WPDesk_Flexible_Shipping_Manifest implements WPDesk_Flexible_Shipping_Manifest_Interface {

        private $shipX = null;

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

        public function get_number() {
            return $this->get_meta( '_number' );
        }

        public function get_manifest() {
            $manifest = array(
                'file_name' => 'inpost_manifest_' . $this->get_number() . '.pdf',
                'content'   => file_get_contents( ABSPATH . '/wp-content/plugins/woocommerce-paczkomaty-inpost/class/manifest.pdf' )
            );
            return $manifest;
        }

        public function generate() {
            $this->set_meta( '_number', rand(1,10000000) );
        }

        public function cancel() {
            $this->delete_meta( '_number' );
        }

        private function get_shipX() {
            if ( $this->shipX == null ) {
                $all_shipping_methods = WC()->shipping()->get_shipping_methods();
                $paczkomaty_shipping_method = $all_shipping_methods['paczkomaty_shipping_method'];
                $test_mode = true;
                if ( $paczkomaty_shipping_method->get_option( 'test_mode', '' ) != 'yes' ) {
                    $test_mode = false;
                }
                $organization_id = $paczkomaty_shipping_method->get_option( 'organization_id', '' );
                $this->shipX     = new WPDesk_Paczkomaty_ShipX( $paczkomaty_shipping_method->get_option( 'token', '' ), $paczkomaty_shipping_method->get_option( 'token_test_mode', '' ), $test_mode );
                $this->shipX->set_organization_id( $organization_id );
            }
            return $this->shipX;
        }


    }

}
