let{remote,ipcRenderer}=require('electron');function getConfigInstance(){let Config=require('conf');return new Config('yout-playlist');}
function deletePlaylist(id){let configuration=getConfigInstance();let message='Press ok to delete the '+configuration.get('playlist.'+id+'.name')+' playlist.';if(confirm(message)==true&&configuration.has('playlist.'+id)){configuration.delete('playlist.'+id);ipcRenderer.send('update-playlist');setTimeout(function(){remote.getCurrentWindow().reload();},200);configuration=null;}}
function loadPlaylistItems(){let configuration=getConfigInstance();playlistsKeys=configuration.get('playlist');for(let value in playlistsKeys){let playlistTable=document.getElementById('table');let playlistTr=document.createElement('tr');let playlistTd=document.createElement('td');let playlistTd2=document.createElement('td');let button=document.createElement('button');playlistTable.appendChild(playlistTr);playlistTr.appendChild(playlistTd);playlistTr.appendChild(playlistTd2);playlistTd.innerHTML=configuration.get('playlist.'+value+'.name');button.setAttribute('id',value);button.setAttribute('class','panel-buttons fa fa-trash');button.setAttribute('onclick','deletePlaylist("'+value+'")');playlistTd2.appendChild(button);}
configuration=null;}
function makeId(playlist){let searchFirstKey=playlist.get('playlist');let foundedKey;for(let value in searchFirstKey){if(playlist.has('playlist.'+value)){foundedKey=value;}}
return Number(foundedKey)+1;}
function insertPlaylist(){let configuration=getConfigInstance();let name=document.getElementById('name').value;let url=document.getElementById('url').value;let id=0;if(configuration.has('playlist')&&Object.keys(configuration.get('playlist')).length>0){id=makeId(configuration);}else{id=1;}
if(id<=8&&url.indexOf('youtube')!==-1&&url.indexOf('list')!==-1&&name.length>0&&url.length>0){configuration.set('playlist.'+id+'.name',name);configuration.set('playlist.'+id+'.url',url);ipcRenderer.send('update-playlist');setTimeout(function(){remote.getCurrentWindow().reload();},200);configuration=null;}else{alert('Something wrong, check if the url is valid or if the limit of 8 playlists was not reached. :(');}}
function hide(){remote.getCurrentWindow().destroy();}
