<?php
// don't call the file directly
defined( 'ABSPATH' ) || exit();

class WPCP_Etsy extends WPCP_Module {

    /**
     * The single instance of the class
     *
     * @var $this ;
     */
    protected static $_instance = null;

    /**
     * WPCP_Module constructor.
     */
    public function __construct() {
        //option fields
        add_action( 'wpcp_etsy_campaign_options_meta_fields', 'wpcp_keyword_suggestion_field' );
        add_action( 'wpcp_etsy_campaign_options_meta_fields', 'wpcp_keyword_field' );
        parent::__construct( 'etsy' );
    }

    /**
     * @return string
     */
    public function get_module_icon() {
        return '';
    }

    /**
     * @return array
     * @since 1.1.7
     */
    public function get_template_tags() {
        return array(
            'title'         => __( 'Title', 'wp-content-pilot-pro' ),
            'content'       => __( 'Content', 'wp-content-pilot-pro' ),
            'image_url'     => __( 'Main image url', 'wp-content-pilot-pro' ),
            'source_url'    => __( 'Source link', 'wp-content-pilot-pro' ),
            'quantity'      => __( 'Quantity', 'wp-content-pilot-pro' ),
            'price'         => __( 'Price', 'wp-content-pilot-pro' ),
            'tags'          => __( 'Associated Tags', 'wp-content-pilot-pro' ),
            'category'      => __( 'Category', 'wp-content-pilot-pro' ),
            'materials'     => __( 'Materials', 'wp-content-pilot-pro' ),
            'sku'           => __( 'SKU', 'wp-content-pilot-pro' ),
            'views'         => __( 'Views', 'wp-content-pilot-pro' ),
            'favourites'    => __( 'Favourites', 'wp-content-pilot-pro' ),
            'variations'    => __( 'Variations', 'wp-content-pilot-pro' ),
            'creation_time' => __( 'Creation Time', 'wp-content-pilot-pro' ),
            'ending_time'   => __( 'Ending Time', 'wp-content-pilot-pro' ),
        );
    }

    /**
     * @return string
     * @since 1.1.7
     */
    public function get_default_template() {
        $template =
            <<<EOT
<img src="{image_url}">
{content}
<br> <a href="{source_url}" target="_blank">Source</a>
EOT;

        return $template;
    }

    /**
     * @param $post
     *
     * @return 1.1.7
     */
    public function add_campaign_option_fields( $post ) {

        echo WPCP_HTML::start_double_columns();
        echo WPCP_HTML::select_input( array(
            'name'    => '_etsy_category',
            'label'   => __( 'Select category to search products', 'wp-content-pilot-pro' ),
            'options' => $this->get_etsy_categories(),
            'class'   => 'wpcp-select2',
            'tooltip' => __( 'Select the categories for specific search', 'wp-content-pilot-pro' ),
        ) );
        echo WPCP_HTML::select_input( array(
            'name'    => '_etsy_region',
            'label'   => __( 'Select Region', 'wp-content-pilot-pro' ),
            'class'   => 'wpcp-select2',
            'options' => $this->get_supported_regions(),
            'tooltip' => __( 'Select Regions for specific search results', 'wp-content-pilot-pro' ),
        ) );
        echo WPCP_HTML::select_input( array(
            'name'    => '_etsy_language',
            'label'   => __( 'Select Language', 'wp-content-pilot-pro' ),
            'class'   => 'wpcp-select2',
            'options' => $this->get_supported_language(),
            'default' => 'en',
            'tooltip' => __( 'Select Language for specific search results', 'wp-content-pilot-pro' ),
        ) );
        echo WPCP_HTML::end_double_columns();

    }

    /**
     * @param $campaign_id
     * @param $posted
     */
    public function save_campaign_meta( $campaign_id, $posted ) {
        update_post_meta( $campaign_id, '_etsy_category', empty( $posted['_etsy_category'] ) ? '' : sanitize_key( $posted['_etsy_category'] ) );
        update_post_meta( $campaign_id, '_etsy_region', empty( $posted['_etsy_region'] ) ? '' : $posted['_etsy_region'] );
        update_post_meta( $campaign_id, '_etsy_language', empty( $posted['_etsy_language'] ) ? '' : $posted['_etsy_language'] );
    }

    /**
     * @param $section
     *
     * @return array
     * @since 1.2.0
     */
    public function get_setting_section( $sections ) {
        $sections[] = [
            'id'    => 'wpcp_settings_etsy',
            'title' => __( 'Etsy Settings', 'wp-content-pilot-pro' )
        ];

        return $sections;
    }

    /**
     * @param $fields
     *
     * @return array
     * @since 1.2.0
     */
    public function get_setting_fields( $fields ) {
        $fields['wpcp_settings_etsy'] = [
            array(
                'name'  => '_etsy_api',
                'label' => __( 'Api Key', 'wp-content-pilot-pro' ),
                'desc'  => sprintf( __( '<a href="%s" target="_blank">Here</a> is the guide to get your Etsy API key.', 'wp-content-pilot-pro' ), 'https://pluginever.com/docs/wp-content-pilot/etsy-campaign-settings/' ),
                'type'  => 'password',
            ),
        ];

        return $fields;
    }


    /**
     * @param int $campaign_id
     *
     * @return array|mixed|WP_Error
     * @throws ErrorException
     * @since 1.1.7
     */
    public function get_post( $campaign_id ) {
        wpcp_logger()->info( __( 'Loaded Etsy Campaign', 'wp-content-pilot-pro' ), $campaign_id );

        $api_key = wpcp_get_settings( '_etsy_api', 'wpcp_settings_etsy' );
        wpcp_logger()->info( __( 'Checking Etsy api key and api secret for authentication', 'wp-content-pilot-pro' ), $campaign_id );
        if ( empty( $api_key ) ) {
            wpcp_disable_campaign( $campaign_id );
            $notice = __( 'Etsy api key is not set.So, the campaign wont run, disabling campaign.', 'wp-content-pilot-pro' );
            wpcp_logger()->error( $notice, $campaign_id );

            return new WP_Error( 'missing-data', $notice );
        }

        //it can be anything
        $keywords = $this->get_campaign_meta( $campaign_id );
        if ( empty( $keywords ) ) {
            return new WP_Error( 'missing-data', __( 'Campaign do not have keyword to proceed, please set keyword', 'wp-content-pilot-pro' ) );
        }

        //loop through keywords
        foreach ( $keywords as $keyword ) {
            wpcp_logger()->info( sprintf( __( 'Looping through keywords now trying with keyword [ %s ]', 'wp-content-pilot-pro' ), $keyword ), $campaign_id );

            if ( $this->is_deactivated_key( $campaign_id, $keyword ) ) {
                wpcp_logger()->error( sprintf( __( 'The keyword is deactivated for 1 hr because last time could not find any article with keyword [%s]', 'wp-content-pilot-pro' ), $keyword ), $campaign_id );
                continue;
            }

            //get links from database
            wpcp_logger()->info( __( 'Checking existing links in store...', 'wp-content-pilot-pro' ), $campaign_id );
            $links = $this->get_links( $keyword, $campaign_id );
            if ( empty( $links ) ) {
                wpcp_logger()->info( __( 'No cached links in store. Generating new links...', 'wp-content-pilot-pro' ), $campaign_id );
                $this->discover_links( $campaign_id, $keyword );
                $links = $this->get_links( $keyword, $campaign_id );
            }

            foreach ( $links as $link ) {
                wpcp_logger()->info( sprintf( __( 'Grabbing article from [%s]', 'wp-content-pilot-pro' ), $link->url ), $campaign_id );

                $this->update_link( $link->id, [ 'status' => 'failed', 'meta' => '' ] );

                $curl = $this->setup_curl();
                $curl->get( $link->url );
                wpcp_logger()->info( sprintf( __( 'Requesting content from links [%s]', 'wp-content-pilot-pro' ), $link->url ), $campaign_id );

                if ( $curl->isError() && $this->initiator != 'cron' ) {
                    wpcp_logger()->info( sprintf( __( "Failed processing link reason [%s]", 'wp-content-pilot-pro' ), $curl->getErrorMessage() ), $campaign_id );
                    continue;
                }

                $html = $curl->response;

                $readability = new WPCP_Readability();
                $readable    = $readability->parse( $html, $link->url );

                if ( is_wp_error( $readable ) ) {
                    wpcp_logger()->info( sprintf( "Failed readability reason [%s] changing to different link", $readable->get_error_message() ), $campaign_id );
                    continue;
                }

                //check if the clean title metabox is checked and perform title cleaning
                $check_clean_title = wpcp_get_post_meta( $campaign_id, '_clean_title', 'off' );

                if ( 'on' == $check_clean_title ) {
                    wpcp_logger()->info( __( 'Cleaning title', 'wp-content-pilot-pro' ), $campaign_id );
                    $title = wpcp_clean_title( $link->title );
                } else {
                    $title = html_entity_decode( $link->title, ENT_QUOTES );
                }

                $meta = $link->meta;

                $item_description   = ! empty( $meta['description'] ) ? $meta['description'] : '';
                $price              = $meta['currency_code'] . ' ' . $meta['price'];
                $available_quantity = ! empty( $meta['quantity'] ) ? $meta['quantity'] : '';
                $associated_tags    = ! empty( $meta['tags'] ) ? implode( " , ", $meta['tags'] ) : '';
                $available_category = ! empty( $meta['category'] ) ? implode( " , ", $meta['category'] ) : '';
                $sku                = ! empty( $meta['sku'] ) ? implode( " , ", $meta['sku'] ) : '';
                $materials          = ! empty( $meta['materials'] ) ? implode( " , ", $meta['materials'] ) : '';
                $views              = ! empty( $meta['views'] ) ? $meta['views'] : '';
                $favourites         = ! empty( $meta['favourite_count'] ) ? $meta['favourite_count'] : '';
                $variations         = ! empty( $meta['variations'] ) ? 'Yes' : 'No';
                $creation_time      = ! empty( $meta['creation_time'] ) ? date( "Y-m-d H:i:s", substr( $meta['creation_time'], 0, 10 ) ) : '';
                $ending_time        = ! empty( $meta['ending_time'] ) ? date( "Y-m-d H:i:s", substr( $meta['ending_time'], 0, 10 ) ) : '';

                wpcp_logger()->info( __( 'Combining all parts for article', 'wp-content-pilot-pro' ), $campaign_id );
                $article = array(
                    'title'         => $title,
                    'content'       => $item_description,
                    'image_url'     => $readability->get_image(),
                    'source_url'    => $link->url,
                    'quantity'      => $available_quantity,
                    'price'         => $price,
                    'tags'          => $associated_tags,
                    'category'      => $available_category,
                    'materials'     => $materials,
                    'sku'           => $sku,
                    'views'         => $views,
                    'favourites'    => $favourites,
                    'variations'    => $variations,
                    'creation_time' => $creation_time,
                    'ending_time'   => $ending_time,
                );

                wpcp_logger()->info( __( 'Article processed from campaign', 'wp-content-pilot-pro' ), $campaign_id );
                $this->update_link( $link->id, [ 'status' => 'success', 'meta' => '' ] );

                return $article;
            }

        }

        $log_url = admin_url( '/edit.php?post_type=wp_content_pilot&page=wpcp-logs' );

        return new WP_Error( 'campaign-error', __( sprintf( 'No article generated check <a href="%s">log</a> for details.', $log_url ), 'wp-content-pilot-pro' ) );
    }


    /**
     * @param $campaign_id
     * @param $keyword
     *
     * @return bool|mixed|WP_Error
     * @throws ErrorException
     * @since 1.1.7
     */
    protected function discover_links( $campaign_id, $keyword ) {
        $api_key     = wpcp_get_settings( '_etsy_api', 'wpcp_settings_etsy' );
        $page_key    = sanitize_key( '_page_' . $campaign_id . '_' . md5( $keyword ) );
        $page_number = wpcp_get_post_meta( $campaign_id, $page_key, '1' );
        $category    = wpcp_get_post_meta( $campaign_id, '_etsy_category' );
        $region      = wpcp_get_post_meta( $campaign_id, '_etsy_region' );
        $language    = wpcp_get_post_meta( $campaign_id, '_etsy_language' );


        $api_url = sprintf( 'https://openapi.etsy.com/v2/listings/active/' );

        $query_args           = array(
            'api_key'  => $api_key,
            'keywords' => urlencode( $keyword ),
            'includes' => 'Images',
            'limit'    => 25,
            'page'     => $page_number,
        );
        $query_args['offset'] = ( $page_number - 1 ) * $query_args['limit'];

        if ( $category != null ) {
            $query_args['taxonomy_id'] = $category;
        }
        if ( $region != null ) {
            $query_args['region'] = strtoupper( $region );
        }
        if ( $language != null ) {
            $query_args['language'] = $language;
        }

        $endpoint = add_query_arg( $query_args, $api_url );
        wpcp_logger()->info( sprintf( __( 'Requesting urls from Etsy [%s]', 'wp-content-pilot-pro' ), preg_replace( '/api_key=([^&]+)/m', 'api_key=X', $endpoint ) ) );

        $curl = $this->setup_curl();
        $curl->get( $endpoint );

        if ( $curl->isError() ) {
            $message = sprintf( __( 'Etsy api request failed response [%s]', 'wp-content-pilot-pro' ), $curl->response->errorMessage );
            wpcp_logger()->error( $message );

            return false;
        }

        $response = $curl->getResponse();
        if ( $response->count === 0 ) {
            $message = __( 'No items returned from Etsy, deactivating keyword for an hour.', 'wp-content-pilot-pro' );
            wpcp_logger()->info( $message, $campaign_id );
            $this->deactivate_key( $campaign_id, $keyword );

            return new WP_Error( 'no-links-found', $message );
        }

        $products = $response->results;

        $counter = 0;
        wpcp_logger()->info( __( 'Looping through links for storing..', 'wp-content-pilot-pro' ), $campaign_id );
        foreach ( $products as $product ) {
            $meta                    = [];
            $meta['listing_id']      = $product->listing_id;
            $meta['description']     = $product->description;
            $meta['price']           = $product->price;
            $meta['currency_code']   = $product->currency_code;
            $meta['tags']            = $product->tags;
            $meta['quantity']        = $product->quantity;
            $meta['category']        = $product->taxonomy_path;
            $meta['materials']       = $product->materials;
            $meta['sku']             = $product->sku;
            $meta['views']           = $product->views;
            $meta['favourite_count'] = $product->num_favorers;
            $meta['variations']      = $product->has_variations;
            $meta['creation_time']   = $product->creation_tsz;
            $meta['ending_time']     = $product->ending_tsz;

            $title     = $product->title;
            $item_link = $product->url;

            if ( wpcp_is_duplicate_url( $item_link ) ) {
                continue;
            }

            //check duplicate title and don't publish the post with duplicate title
            $skip = apply_filters( 'wpcp_skip_duplicate_title', true, $title, $campaign_id );
            if ( $skip ) {
                wpcp_logger()->info( __( 'Duplicate title skipping', 'wp-content-pilot-pro' ), $campaign_id );
                continue;
            }
            $data = [
                'url'     => esc_url( $item_link ),
                'title'   => $title,
                'for'     => $keyword,
                'camp_id' => $campaign_id,
                'meta'    => $meta,
            ];

            if ( ! false == $this->insert_link( $data ) ) {
                $counter += 1;
            };
        }
        wpcp_update_post_meta( $campaign_id, $page_key, $page_number + 1 );
        wpcp_logger()->info( sprintf( __( 'Total found links [%d] and accepted [%d] and rejected [%d]', 'wp-content-pilot-pro' ), count( $products ), $counter, ( count( $products ) - $counter ) ), $campaign_id );

        return true;
    }

    /**
     * Get all etsy categories for search
     *
     * @return array
     * @since 1.1.7
     *
     */

    public function get_etsy_categories() {
        $categories = array(
            '1'     => __( 'Accessories', 'wp-content-pilot-pro' ),
            '1728'  => __( 'Baby Accessories', 'wp-content-pilot-pro' ),
            '2'     => __( 'Belts & Suspenders', 'wp-content-pilot-pro' ),
            '10783' => __( 'Bouquets & Corsages', 'wp-content-pilot-pro' ),
            '6'     => __( 'Costume Accessories', 'wp-content-pilot-pro' ),
            '16'    => __( 'Gloves & Mittens', 'wp-content-pilot-pro' ),
            '219'   => __( 'Hair Accessories', 'wp-content-pilot-pro' ),
            '11245' => __( 'Hand Fans', 'wp-content-pilot-pro' ),
            '25'    => __( 'Hats & Caps', 'wp-content-pilot-pro' ),
            '164'   => __( 'Keychains & Lanyards', 'wp-content-pilot-pro' ),
            '39'    => __( 'Patches & Pins', 'wp-content-pilot-pro' ),
            '42'    => __( 'Scarves & Wraps', 'wp-content-pilot-pro' ),
            '48'    => __( 'Suit & Tie Accessories', 'wp-content-pilot-pro' ),
            '58'    => __( 'Sunglasses & Eyewear', 'wp-content-pilot-pro' ),
            '65'    => __( 'Umbrellas & Rain Accessories', 'wp-content-pilot-pro' ),
            '66'    => __( 'Art & Collectibles', 'wp-content-pilot-pro' ),
            '1865'  => __( 'Artist Trading Cards', 'wp-content-pilot-pro' ),
            '67'    => __( 'Collectibles', 'wp-content-pilot-pro' ),
            '1612'  => __( 'Dolls & Miniatures', 'wp-content-pilot-pro' ),
            '75'    => __( 'Drawing & Illustration', 'wp-content-pilot-pro' ),
            '82'    => __( 'Fiber Arts', 'wp-content-pilot-pro' ),
            '6096'  => __( 'Fine Art Ceramics', 'wp-content-pilot-pro' ),
            '104'   => __( 'Glass Art', 'wp-content-pilot-pro' ),
            '99'    => __( 'Mixed Media & Collage', 'wp-content-pilot-pro' ),
            '105'   => __( 'Painting', 'wp-content-pilot-pro' ),
            '115'   => __( 'Photography', 'wp-content-pilot-pro' ),
            '119'   => __( 'Prints', 'wp-content-pilot-pro' ),
            '128'   => __( 'Sculpture', 'wp-content-pilot-pro' ),
            '132'   => __( 'Bags & Purses', 'wp-content-pilot-pro' ),
            '133'   => __( 'Accessory Cases', 'wp-content-pilot-pro' ),
            '136'   => __( 'Backpacks', 'wp-content-pilot-pro' ),
            '137'   => __( 'Clothing & Shoe Bags', 'wp-content-pilot-pro' ),
            '141'   => __( 'Cosmetic & Toiletry Storage', 'wp-content-pilot-pro' ),
            '149'   => __( 'Diaper Bags', 'wp-content-pilot-pro' ),
            '1700'  => __( 'Electronics Cases', 'wp-content-pilot-pro' ),
            '150'   => __( 'Fanny Packs', 'wp-content-pilot-pro' ),
            '151'   => __( 'Bags & Purses', 'wp-content-pilot-pro' ),
            '156'   => __( 'Handbags', 'wp-content-pilot-pro' ),
            '2863'  => __( 'Keychains & Lanyards', 'wp-content-pilot-pro' ),
            '168'   => __( 'Luggage & Travel', 'wp-content-pilot-pro' ),
            '180'   => __( 'Market Bags', 'wp-content-pilot-pro' ),
            '181'   => __( 'Messenger Bags', 'wp-content-pilot-pro' ),
            '182'   => __( 'Pouches & Coin Purses', 'wp-content-pilot-pro' ),
            '183'   => __( 'Sports Bags', 'wp-content-pilot-pro' ),
            '190'   => __( 'Totes', 'wp-content-pilot-pro' ),
            '191'   => __( 'Wallets & Money Clips', 'wp-content-pilot-pro' ),
            '199'   => __( 'Bath & Beauty', 'wp-content-pilot-pro' ),
            '200'   => __( 'Baby & Child Care', 'wp-content-pilot-pro' ),
            '210'   => __( 'Bath Accessories', 'wp-content-pilot-pro' ),
            '1710'  => __( 'Cosmetic & Toiletry Storage', 'wp-content-pilot-pro' ),
            '215'   => __( 'Essential Oils', 'wp-content-pilot-pro' ),
            '216'   => __( 'Fragrances', 'wp-content-pilot-pro' ),
            '217'   => __( 'Hair Care', 'wp-content-pilot-pro' ),
            '241'   => __( 'Makeup & Cosmetics', 'wp-content-pilot-pro' ),
            '270'   => __( 'Personal Care', 'wp-content-pilot-pro' ),
            '292'   => __( 'Skin Care', 'wp-content-pilot-pro' ),
            '308'   => __( 'Soaps', 'wp-content-pilot-pro' ),
            '316'   => __( 'Spa & Relaxation', 'wp-content-pilot-pro' ),
            '323'   => __( 'Books, Movies & Music', 'wp-content-pilot-pro' ),
            '324'   => __( 'Books', 'wp-content-pilot-pro' ),
            '355'   => __( 'Movies', 'wp-content-pilot-pro' ),
            '356'   => __( 'Music', 'wp-content-pilot-pro' ),
            '373'   => __( 'Video Cases & Tins', 'wp-content-pilot-pro' ),
            '374'   => __( 'Clothing', 'wp-content-pilot-pro' ),
            '375'   => __( 'Boys\' Clothing', 'wp-content-pilot-pro' ),
            '401'   => __( 'Girls\' Clothing', 'wp-content-pilot-pro' ),
            '432'   => __( 'Men\'s Clothing', 'wp-content-pilot-pro' ),
            '484'   => __( 'Unisex Kids\' Clothing', 'wp-content-pilot-pro' ),
            '500'   => __( 'Women\'s Clothing', 'wp-content-pilot-pro' ),
            '562'   => __( 'Craft Supplies & Tools', 'wp-content-pilot-pro' ),
            '9317'  => __( 'Home & Hobby', 'wp-content-pilot-pro' ),
            '9318'  => __( 'Jewelry & Beauty', 'wp-content-pilot-pro' ),
            '9314'  => __( 'Paper, Party & Kids', 'wp-content-pilot-pro' ),
            '9316'  => __( 'Sculpting & Forming', 'wp-content-pilot-pro' ),
            '9315'  => __( 'Sewing & Fiber', 'wp-content-pilot-pro' ),
            '9313'  => __( 'Visual Arts', 'wp-content-pilot-pro' ),
            '825'   => __( 'Electronics & Accessories', 'wp-content-pilot-pro' ),
            '826'   => __( 'Audio', 'wp-content-pilot-pro' ),
            '832'   => __( 'Batteries & Charging', 'wp-content-pilot-pro' ),
            '835'   => __( 'Cables & Cords', 'wp-content-pilot-pro' ),
            '838'   => __( 'Cameras', 'wp-content-pilot-pro' ),
            '846'   => __( 'Car Parts & Accessories', 'wp-content-pilot-pro' ),
            '847'   => __( 'Cell Phone Accessories', 'wp-content-pilot-pro' ),
            '850'   => __( 'Computers & Peripherals', 'wp-content-pilot-pro' ),
            '860'   => __( 'DIY Kits', 'wp-content-pilot-pro' ),
            '855'   => __( 'Decals & Skins', 'wp-content-pilot-pro' ),
            '861'   => __( 'Docking & Stands', 'wp-content-pilot-pro' ),
            '865'   => __( 'Electronics Cases', 'wp-content-pilot-pro' ),
            '875'   => __( 'Gadgets', 'wp-content-pilot-pro' ),
            '876'   => __( 'Maker Supplies', 'wp-content-pilot-pro' ),
            '880'   => __( 'Parts & Electrical', 'wp-content-pilot-pro' ),
            '886'   => __( 'TV & Projection', 'wp-content-pilot-pro' ),
            '885'   => __( 'Telephones & Handsets', 'wp-content-pilot-pro' ),
            '890'   => __( 'Video Games', 'wp-content-pilot-pro' ),
            '891'   => __( 'Home & Living', 'wp-content-pilot-pro' ),
            '900'   => __( 'Bathroom', 'wp-content-pilot-pro' ),
            '911'   => __( 'Bedding', 'wp-content-pilot-pro' ),
            '919'   => __( 'Cleaning Supplies', 'wp-content-pilot-pro' ),
            '1178'  => __( 'Curtains & Window Treatments', 'wp-content-pilot-pro' ),
            '927'   => __( 'Floor & Rugs', 'wp-content-pilot-pro' ),
            '930'   => __( 'Food & Drink', 'wp-content-pilot-pro' ),
            '967'   => __( 'Furniture', 'wp-content-pilot-pro' ),
            '892'   => __( 'Home Appliances', 'wp-content-pilot-pro' ),
            '1002'  => __( 'Home Décor', 'wp-content-pilot-pro' ),
            '779'   => __( 'Home Improvement', 'wp-content-pilot-pro' ),
            '1031'  => __( 'Kitchen & Dining', 'wp-content-pilot-pro' ),
            '1086'  => __( 'Lighting', 'wp-content-pilot-pro' ),
            '1096'  => __( 'Office', 'wp-content-pilot-pro' ),
            '1105'  => __( 'Outdoor & Gardening', 'wp-content-pilot-pro' ),
            '1135'  => __( 'Spirituality & Religion', 'wp-content-pilot-pro' ),
            '1165'  => __( 'Storage & Organization', 'wp-content-pilot-pro' ),
            '1179'  => __( 'Jewelry', 'wp-content-pilot-pro' ),
            '1181'  => __( 'Body Jewelry', 'wp-content-pilot-pro' ),
            '1193'  => __( 'Bracelets', 'wp-content-pilot-pro' ),
            '10840' => __( 'Brooches, Pins & Clips', 'wp-content-pilot-pro' ),
            '10842' => __( 'Cremation & Memorial Jewelry', 'wp-content-pilot-pro' ),
            '2317'  => __( 'Cuff Links & Tie Clips', 'wp-content-pilot-pro' ),
            '1203'  => __( 'Earrings', 'wp-content-pilot-pro' ),
            '2115'  => __( 'Jewelry Sets', 'wp-content-pilot-pro' ),
            '1216'  => __( 'Jewelry Storage', 'wp-content-pilot-pro' ),
            '1217'  => __( 'Necklaces', 'wp-content-pilot-pro' ),
            '1231'  => __( 'Rings', 'wp-content-pilot-pro' ),
            '1249'  => __( 'Watches', 'wp-content-pilot-pro' ),
            '6110'  => __( 'Wearable Tech Jewelry', 'wp-content-pilot-pro' ),
            '1250'  => __( 'Paper & Party Supplies', 'wp-content-pilot-pro' ),
            '1251'  => __( 'Paper', 'wp-content-pilot-pro' ),
            '1330'  => __( 'Party Supplies', 'wp-content-pilot-pro' ),
            '1351'  => __( 'Pet Supplies', 'wp-content-pilot-pro' ),
            '1352'  => __( 'Beekeeping', 'wp-content-pilot-pro' ),
            '1353'  => __( 'Pet Bedding', 'wp-content-pilot-pro' ),
            '1358'  => __( 'Pet Carriers & Houses', 'wp-content-pilot-pro' ),
            '1368'  => __( 'Pet Clothing, Accessories & Shoes', 'wp-content-pilot-pro' ),
            '1383'  => __( 'Pet Collars & Leashes', 'wp-content-pilot-pro' ),
            '1388'  => __( 'Pet Feeding', 'wp-content-pilot-pro' ),
            '1394'  => __( 'Pet Furniture', 'wp-content-pilot-pro' ),
            '1401'  => __( 'Pet Gates & Fences', 'wp-content-pilot-pro' ),
            '1402'  => __( 'Pet Health & Wellness', 'wp-content-pilot-pro' ),
            '1415'  => __( 'Pet Storage', 'wp-content-pilot-pro' ),
            '1416'  => __( 'Pet Toys', 'wp-content-pilot-pro' ),
            '1421'  => __( 'Riding & Farm Animals', 'wp-content-pilot-pro' ),
            '1422'  => __( 'Training', 'wp-content-pilot-pro' ),
            '1425'  => __( 'Urns & Memorials', 'wp-content-pilot-pro' ),
            '1429'  => __( 'Shoes', 'wp-content-pilot-pro' ),
            '1430'  => __( 'Boys\' Shoes', 'wp-content-pilot-pro' ),
            '1440'  => __( 'Girls\' Shoes', 'wp-content-pilot-pro' ),
            '1451'  => __( 'Insoles & Accessories', 'wp-content-pilot-pro' ),
            '1458'  => __( 'Men\'s Shoes', 'wp-content-pilot-pro' ),
            '1512'  => __( 'Women\'s Shoes', 'wp-content-pilot-pro' ),
            '1552'  => __( 'Toys & Games', 'wp-content-pilot-pro' ),
            '1553'  => __( 'Games & Puzzles', 'wp-content-pilot-pro' ),
            '1560'  => __( 'Sports & Outdoor Recreation', 'wp-content-pilot-pro' ),
            '1580'  => __( 'Toys', 'wp-content-pilot-pro' ),
            '1633'  => __( 'Weddings', 'wp-content-pilot-pro' ),
            '1634'  => __( 'Accessories', 'wp-content-pilot-pro' ),
            '1652'  => __( 'Clothing', 'wp-content-pilot-pro' ),
            '1657'  => __( 'Decorations', 'wp-content-pilot-pro' ),
            '1664'  => __( 'Gifts & Mementos', 'wp-content-pilot-pro' ),
            '1672'  => __( 'Invitations & Paper', 'wp-content-pilot-pro' ),
            '1680'  => __( 'Jewelry', 'wp-content-pilot-pro' ),
            '1696'  => __( 'Shoes', 'wp-content-pilot-pro' ),
        );

        return $categories;
    }

    /**
     * Get all supported regions for search
     *
     * @return array
     * @since 1.1.7
     *
     */
    public function get_supported_regions() {
        return array(
            ''   => 'Select Regions',
            'US' => 'United States of America',
            'CA' => 'Canada',
            'AU' => 'Australia',
            'GB' => 'United Kingdom',
            'DE' => 'Germany',
            'FR' => 'France',
            'NL' => 'The Netherlands',
            'IT' => 'Italy',
            'ES' => 'Spain',
            'RU' => 'Russia',
            'PT' => 'Portugal',
            'GR' => 'Greece',
            'IE' => 'Ireland',
            'JP' => 'Japan',
            'NZ' => 'New Zealand',
            'PL' => 'Poland',
        );
    }

    /**
     * Get all supported language for search
     *
     * @return array
     * @since 1.1.7
     *
     */
    public function get_supported_language() {
        return array(
            'de' => 'German',
            'en' => 'English',
            'es' => 'Spanish',
            'fr' => 'French',
            'it' => 'Italian',
            'ja' => 'Japanese',
            'nl' => 'Dutch',
            'pl' => 'Polish',
            'pt' => 'Portuguese',
            'ru' => 'Russian',
        );
    }


    /**
     * Main WPCP_Etsy Instance.
     *
     * Ensures only one instance of WPCP_Etsy is loaded or can be loaded.
     *
     * @return WPCP_Etsy Main instance
     * @since 1.0.0
     * @static
     */
    public static function instance() {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}

WPCP_Etsy::instance();
