/**
 * Copyright (c) 2017 Gian MR
 * Gian MR Theme Custom Javascript
 *
 * @package Dlpro Autopost
 */

/* grabbing */
function dlproap_trigger_grab( url ) {
	jQuery( "#poststuff #dlproap_source" ).val( url );
	dlproap_js_grab();
}

function dlproap_js_grab() {
	var target       = document.URL;
	var url_position = jQuery( "#dlproap_source" ).offset().top - 100;

	var url = jQuery( "#dlproap_source" ).val();
	var pageidbool = jQuery( "#pageidbool" ).val();
	if ( url == '' ) {
		alert( "Grabber aborted !, You did not fill any url :(" );
		jQuery( "#dlproap_source" ).attr( 'Please input url here!' ).focus();
		return false;
	}

	jQuery( "html, body" ).animate( { scrollTop: url_position }, "slow" );
	jQuery( "#dlproap_grab_button" ).text( "Wait.." ).prop( "disabled",true );
	jQuery( "#dlproap_source" ).prop( "disabled",true );
	var data = {
		'action':'dlpro_autopost_grab_content',
		'url':url,
		'pageidbool':pageidbool,
	};
	jQuery.ajax(
		{
			data: data,
			type: "POST",
			timeout: 15000,
			url: dlproap_object.ajax_url,
			success:function( res ) {
				if ( res.api_status == 0 ) {
					alert( res.api_message );
					jQuery( "#dlproap_grab_button" ).text( "Grab URL" ).prop( "disabled",false );
					jQuery( "#dlproap_source" ).prop( "disabled",false );
					return false;
				}

				if ( res.title !== '' ) {
					jQuery( "input[name=post_title]" ).val( res.title ).blur();
					jQuery( "input[name=dlpro-core-AppName-value]" ).val( res.title ).blur();
					jQuery( "input#exthemes_dlbutton_item_title" ).val( res.title ).blur();
					jQuery( "textarea#post-title-0" ).val( res.title ).blur();
					jQuery( "input[name=dlpro-core-wp_title_GP-value]" ).val( res.title ).blur();
					jQuery( "input[name=newtag]" ).val( res.title ).blur();
					jQuery( "input#new-tag-post_tag" ).val( ' ' +res.title+ ',' ).blur();
					//jQuery( "table.dlpro-dlbutton-item tr:first-child input[type=url]" ).val( res.gplayurl ).blur();

				}

				if ( res.content !== '' ) {
					if ( typeof tinyMCE != "undefined" ) {
						var editor_id = wpActiveEditor;
						var video     = '';
						/*
						if ( res.video !== '' ) {
							video = '<br />[embed]' + res.video + '[/embed]';
						}
						*/
						if ( jQuery( '#wp-' + editor_id + '-wrap' ).hasClass( 'tmce-active' ) && tinyMCE.get( editor_id ) ) {
							tinyMCE.get( editor_id ).setContent( res.content + video );
						} else {
							jQuery( "textarea[name=content]" ).val( res.content + video );
							jQuery( "p.block-editor-rich-text__editable block-editor-block-list__block wp-block is-selected rich-text" ).val( res.content + video );
						}
					}
				}
				
				if ( res.content !== '' ) {
					jQuery( "p.block-editor-rich-text__editable block-editor-block-list__block wp-block is-selected rich-text" ).val( res.content );
				}

				// Insert image using ajax.
				if ( res.image !== '' ) {
					if ( ! jQuery( "input[name=dlpro-core-poster-value]" ).val() ) {
						if ( res.title !== '' ) {
							var imagetitle = res.title;
						} else {
							var imagetitle = '';
						}
						// alert(poster);.
						jQuery.ajax(
							{
								type: "POST",
								url: target,
								data: {
									'poster_url':res.image,
									'title_image':imagetitle,
								},
								success: function(response){
									jQuery( "input[name=dlpro-core-poster-value]" ).val( response );
								}
							}
						);
					}
				}

                // Insert Posters.
                if ( res.image !== '' ) {
                    jQuery( "input[name=dlpro-core-posters-value]" ).val( res.image ).blur();
                }
                // Insert Video.
                if ( res.video !== '' ) {
					var video     = '';
                    jQuery( "input[name=dlpro-core-youtubes-id]" ).val( res.video ).blur();
                }



                // Insert version.
				if ( res.version !== '' ) {
					jQuery( "input[name=dlpro-core-version-value]" ).val( res.version ).blur();
				}
				
				if ( res.tags !== '' ) {					 
					jQuery( "input#new-tag-post_tag" ).val( ' ' +res.title+ ' v' +res.version+ ' , ' +res.title+ ', ' +res.developer+ ',' ).blur();

				}

				// Insert whatsnews.
				/*
				if ( res.whatsnews !== '' ) {
					jQuery( "textarea[name=dlpro-core-whatsnews-value]" ).val( res.whatsnews ).blur();
				}
				*/
				if ( res.whatsnews !== '' ) {
					if ( typeof tinyMCE != "undefined" ) {
						var whatsnews = 'DLPRO_Whatsnews';
						if ( jQuery( '#wp-' + whatsnews + '-wrap' ).hasClass( 'tmce-active' ) && tinyMCE.get( whatsnews ) ) {
							tinyMCE.get( whatsnews ).setContent( res.whatsnews );
						} else {
							jQuery( "textarea[name=dlpro-core-whatsnews-value]" ).val( res.whatsnews );
						}
					}
				}
				// Insert requires.
				if ( res.requires !== '' ) {
					jQuery( "input[name=dlpro-core-requires-value]" ).val( res.requires ).blur();
				}
				
				// Insert updated.
				if ( res.updated !== '' ) {
					jQuery( "input[name=dlpro-core-updated-value]" ).val( res.updated ).blur();
				}

				// Insert playstoreID.
				if ( res.playstoreID !== '' ) {
					jQuery( "input[name=dlpro-core-playstoreID-value]" ).val( res.playstoreID ).blur();
				}

				// Insert size.
				if ( res.size !== '' ) {
					jQuery( "input[name=dlpro-core-filesize-value]" ).val( res.size ).blur();
				}

				// Insert total install.
				if ( res.install !== '' ) {
					jQuery( "input[name=dlpro-core-totalinstalls-value]" ).val( res.install ).blur();
				}

				// Insert price.
				if ( res.price !== '' ) {
					jQuery( "input[name=dlpro-core-price-value]" ).val( res.price ).blur();
				}

				// Insert currency.
				if ( res.currency !== '' ) {
					jQuery( "input[name=dlpro-core-currency-value]" ).val( res.currency ).blur();
				}

				// Insert uri.
				if ( res.gplayurl !== '' ) {
					//jQuery( "table.dlpro-dlbutton-item tr:first-child input[type=text]" ).val( 'Download Via GooglePlay' ).blur();
					jQuery( "input.regular-text.display-block.exthemes_dlbutton_item_title" ).val( res.title ).blur();
					jQuery( "input.regular-text.display-block.exthemes_dlbutton_item_version" ).val( res.version ).blur();
					jQuery( "input.regular-text.display-block.exthemes_dlbutton_item_types" ).val( 'Apk' ).blur();
					jQuery( "input.regular-text.display-block.exthemes_dlbutton_item_size" ).val( res.size ).blur();

					jQuery( "table.dlpro-dlbutton-item tr:first-child input[type=url]" ).val( res.gplayurl ).blur();
				}

				// Insert developer.
				if ( res.developer !== '' ) {
					jQuery( "input[name=dlpro-core-developer-value]" ).val( res.developer ).blur();
				} 

				// Insert system.
				if ( res.system !== '' ) {
					jQuery( "input[name=dlpro-core-system-value]" ).val( res.system ).blur();
				}

				// Insert rating.
				if ( res.rating !== '' ) {
					jQuery( "input[id=rmp-avg]" ).val( res.rating ).blur();
					jQuery( "input[name=dlpro-core-voted-value]" ).val( res.rating ).blur();
				}

				// Insert votes.
				if ( res.votes !== '' ) {
					jQuery( "input[id=rmp-votes]" ).val( res.votes ).blur();
					jQuery( "input[name=dlpro-core-totalrating-value]" ).val( res.votes ).blur();
				}
				/*
				if ( res.rated !== '' ) {
					jQuery( "input[name=dlpro-core-contentRating-value]" ).val( res.rated ).blur();
				}
				if ( res.votedGP !== '' ) {
					jQuery( "input[name=dlpro-core-contentRating-value]" ).val( res.votedGP ).blur();
				}
				*/
				
				 
				
				if ( res.rating !== '' && res.votes !== '' ) {
					jQuery('button.js-rmp-mb-update').click();
				}

				if ( res.category !== '' ) {
					jQuery( "input[name=dlpro-core-genres-value]" ).val( res.category ).blur();
				}

				// Insert Category.
				if ( res.category !== '' ) {
					jQuery('a#category-add-toggle').click();
					jQuery('input[name=newcategory]').val( res.category );
					if ( res.parentcategory !== '' ) {
						jQuery('#newcategory_parent option[value="'+ res.parentcategory +'"]').prop('selected', true);
					}
					jQuery('#category-add-submit').trigger('click');
					jQuery('#category-add-submit').prop("disabled", false);
					jQuery('input[name=newcategory]').val("");
				}
				
				// Insert gallery.
				if ( res.gallery !== '' ) {
					jQuery( "textarea[name=dlpro-core-image-gallery]" ).val( '[exthemes-gallery image_url="' + res.gallery + '"]' ).blur();
				}

				// Insert gallery. gallery[image_url][]
				if ( res.gallery !== '' ) {
					jQuery( 'input.addimg').click();
					jQuery( 'input#imgurls' ).val( res.gallery ).blur();

				}

				jQuery( "#dlproap_grab_button" ).text( "Grab URL" ).prop( "disabled",false );
				jQuery( "#dlproap_source" ).prop( "disabled",false );

				setTimeout(
					function() {
						jQuery( "html, body" ).animate( { scrollTop: 0 }, "slow" );
					},
					600
				);
			},
			error: function(request, status, err) {
				//alert( "Ops sorry : " + status + " " + err );
				jQuery( "#dlproap_grab_button" ).text( "Grab URL" ).prop( "disabled",false );
				jQuery( "#dlproap_source" ).prop( "disabled",false );
			}
		}
	);
}
