package com.atum.tunetube;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

import com.github.axet.vget.VGet;
import com.github.axet.vget.vhs.YouTubeInfo;
import com.github.axet.vget.vhs.YouTubeParser;
import com.madturnip.atum.testtube.R;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);

                    //if(true)
                    //    break;
                    new Thread() {
                        public void run(){
                            try
                            {
                                VGet v = new VGet(new URL("https://www.youtube.com/watch?v=5exA_x2P6G8"));
                                /*File downloadDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                                System.out.println(downloadDir);
                                v.setTargetDir(downloadDir);

                                v.download();*/
                                /*playSong(v.getVideo().getSource().toExternalForm());
                                System.out.println("url: "+v.getVideo().getSource().toExternalForm());
                                System.out.println("Finsished downloading");*/

                                YouTubeInfo info = new YouTubeInfo(new URL("https://www.youtube.com/watch?v=5RNePy_awq0"));

                                YouTubeParser parser = new YouTubeParser();

                                List<YouTubeParser.VideoDownload> list = parser.extractLinks(info);
                                boolean playing = false;
                                for (YouTubeParser.VideoDownload d : list) {
                                    if(d.stream instanceof YouTubeInfo.StreamAudio) {
                                        System.out.println("audio only stream: " + d.stream + " " + d.url);
                                        if(!playing){
                                            playing = true;
                                            playSong(d.url.toString());
                                        }
                                    }
                                }
                                for (YouTubeParser.VideoDownload d : list) {
                                    if(!playing){
                                        playing = true;
                                        playSong(d.url.toString());
                                    }
                                    System.out.println(d.stream + " " + d.url);
                                }

                            } catch(
                                    MalformedURLException e)

                            {
                                e.printStackTrace();
                            }
                        }
                    }.start();
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    private void playSong(String url){
        MediaPlayer mPlayer = null;
        try {
            mPlayer = MediaPlayer.create(this, Uri.parse(url));
            mPlayer.start();
        } catch (Exception e){
            e.printStackTrace();
        }
        mPlayer.start();
    }

}

@@ -319,16 +319,62 @@
 tView;%0A%0A
+import com.atum.tunetube.youtube.YoutubeHttp;%0A
 import c
@@ -494,46 +494,8 @@
 ser;
-%0Aimport com.madturnip.atum.testtube.R;
 %0A%0Aim
@@ -1279,32 +1279,282 @@
               %7B%0A
+                                YoutubeHttp http = new YoutubeHttp();%0A                                //open YoutubeMusic page%0A                                http.openUrl(%22https://www.youtube.com/playlist?list=PLqG_Qt4vmaV0x8qTPdcrUyNQ4MglIIxHB%22);%0A%0A
                 

Test for getting popular youtube music.