<?php
namespace Formipay\Shipping;
use Formipay\Traits\SingletonTrait;

if ( ! defined( 'ABSPATH' ) ) exit;

abstract class Shipping {

    use SingletonTrait;

    abstract public function add_shipping_method($shipping_methods);
    abstract public function add_shipping_settings($fields);
    abstract public function add_shipping_to_order_details($details, $form_id, $order_data);

    protected function __construct() {

        add_filter( 'formipay/global-settings',             [$this, 'add_setting_shipping_menu'], 15 );
        add_filter( 'formipay/product-config',                 [$this, 'add_form_shipping_menu'], 75 );

    }

    public function add_setting_shipping_menu($fields){

        $shipping_settings = [];

        $shipping_settings = apply_filters( 'formipay/global-settings/tab:shipping', $shipping_settings );

        if(!empty($shipping_settings)){
            $fields['shipping'] = array(
                'name' => __('Shipping', 'formipay'),
                'fields' => $shipping_settings
            );
        }

        return $fields;

    }

    public function add_form_shipping_menu($fields) {

        $shipping_methods = apply_filters( 'formipay/product-settings/tab:shipping/method', [
            'free_shipping' => [
                'method' => __( 'Free Shipping', 'formipay' )
            ]
        ] );

        $shipping_options = [];
        $shipping_fields = [];

        foreach($shipping_methods as $id => $shipping){
            // $id = $shipping['id'];
            $label = $shipping['method'];
            if(isset($shipping['courier'])){
                $label .= ' - '.$shipping['courier'];
                if(isset($shipping['service'])){
                    $label .= ' - '.$shipping['service'];
                }
            }
            $shipping_options[$id] = $label;
        }

        $shipping_fields = [
            'shipping_notice' => array(
                'type' => 'notification_message',
                'image' => FORMIPAY_URL . 'admin/assets/img/logistics.png',
                'description' => __( '
                    <h1>No Shipping Method Available</h1> 
                    <p>Shipping methods only for physical product type. If you insist to use shipping method, change your product type first</p>
                    ', 'formipay' ),
                'dependency' => array(
                    'key' => 'product_type',
                    'value' => 'digital',
                    'section' => 'general'
                ),
            ),
            'shipping_method' => array(
                'type' => 'radio',
                'label' => esc_html__('Shipping Methods', 'formipay'),
                'options' => $shipping_options,
                'dependency' => array(
                    'key' => 'product_type',
                    'value' => 'physical',
                    'section' => 'general'
                ),
            )
        ];

        $free_shipping_fields = array(
            'free_shipping_group' => array(
                'type' => 'group_title',
                'label' => __( 'Free Shipping Setup', 'formipay' ),
                'description' => __( 'Will not add any shipping fee to the order', 'formipay' ),
                'dependency' => array(
                    array(
                        'key' => 'product_type',
                        'value' => 'physical',
                        'section' => 'general'
                    ),
                    array(
                        'key' => 'shipping_method',
                        'value' => 'free_shipping'
                    )
                ),
                'dependencies' => '&&',
                'group' => 'started'
            ),
            'free_shipping_label' => array(
                'type' => 'text',
                'label' => __( 'Label', 'formipay' ),
                'value' => __( 'Free Shipping', 'formipay' ),
                'dependency' => array(
                    array(
                        'key' => 'product_type',
                        'value' => 'physical',
                        'section' => 'general'
                    ),
                    array(
                        'key' => 'shipping_method',
                        'value' => 'free_shipping'
                    )
                ),
                'dependencies' => '&&',
            ),
            'free_shipping_add_to_order_review' => array(
                'type' => 'checkbox',
                'label' => __( 'Show in Order Review', 'formipay' ),
                'dependency' => array(
                    array(
                        'key' => 'product_type',
                        'value' => 'physical',
                        'section' => 'general'
                    ),
                    array(
                        'key' => 'shipping_method',
                        'value' => 'free_shipping'
                    )
                ),
                'dependencies' => '&&',
                'group' => 'ended'
            ),
        );

        foreach($free_shipping_fields as $key => $value) {
            $shipping_fields[$key] = $value;
        }

        $shipping_fields = apply_filters( 'formipay/product-settings/tab:shipping', $shipping_fields );

        if(!empty($shipping_fields)){
            $fields['formipay_product_settings']['shipping'] = array(
                'name'      => __( 'Shipping', 'formipay' ),
                'fields'    => $shipping_fields
            );
        }

        return $fields;
        
    }

}