class TYTD.NoConvertVideoDownload : IVideoDownload {
    private info;
    private tytd;
    private progress;
    private video_stream_url;
    private audio_stream_url;

    private done=false;
    
    
    public NoConvertVideoDownload(id)
    {
        this.info  = {
            Title = "",
		    Channel = "",
		    VideoId = TYTD.GetVideoId(id),
		    ChannelId = ""
        };
    }
    public getVideo()
    {
        return this.info;
    }
    public setTYTD(tytd)
    {
        this.tytd = tytd;
        var id = this.info.VideoId;
        var path = /"Streams"/id.Substring(0,4)/id.Substring(4)/"vo.bin";
        var pathA = /"Streams"/id.Substring(0,4)/id.Substring(4)/"ao.bin";
        if(this.tytd.Storage.FileExists(path) && this.tytd.Storage.FileExists(pathA)) {
            this.done = true;
            return tytd;
        }

        var req = this.tytd.ManifestRequest(id).playerResponse;
       
        
        this.info.Title = req.videoDetails.title;
        tytd.LOG($"Downloading: {this.info.Title} with id: {id} Highest Video/Audio");
        Console.WriteLine($"Downloading: {this.info.Title} with id: {id} Highest Video/Audio");
        this.info.Channel = req.videoDetails.author;
        this.info.ChannelId = req.videoDetails.channelId;

        this.tytd.PutVideoInfo(req.videoDetails);

        var width = 0;
        var height = 0;
        var sampleRate = 0;
        var bitrate = 0;
        var vurl = "";
        var aurl = "";

        each(var item : req.streamingData.adaptiveFormats)
        {
            if(TypeOf(item.height) == "Long")
            {
                if(item.width >= width && item.height >= height)
                {
                    vurl = item.url;
                    width = item.width;
                    height = item.height;
                }
            }
            else {
                item.audioSampleRate = ParseLong(item.audioSampleRate);
                if(item.audioSampleRate >= sampleRate && item.bitrate >= bitrate)
                {
                    aurl = item.url;
                    sampleRate = item.audioSampleRate;
                    bitrate = item.bitrate;
                }
            }
        }
        
        this.video_stream_url = vurl;
        this.audio_stream_url = aurl;
       
        return tytd;
    }
    public setProgress(p)
    {
        this.progress = p;
    }
    public Start()
    {
        if(this.done) return;
        
        var id = this.info.VideoId;
        var path = /"Streams"/id.Substring(0,4)/id.Substring(4)/"vo.bin";
        var pathA = /"Streams"/id.Substring(0,4)/id.Substring(4)/"ao.bin";
    
        if(!this.tytd.Storage.FileExists(path))
        {
            for(var i = 0; i < 5; i++)
            {
                var req = {
                    FollowRedirects = true,
                    RequestHeaders = [
                            {
                                Key = "User-Agent",
                                
                                Value = "com.google.android.youtube/19.28.35 (Linux; U; Android 15; GB) gzip"
                            }
                    ]
                };
                var resp = Net.Http.MakeRequest(this.video_stream_url,req);
        
                if(resp.StatusCode >= 200 && resp.StatusCode <= 299)
                {
                    this.tytd.Storage.CreateDirectory(path.GetParent());
                    var strm = this.tytd.Storage.OpenFile(path+".part","wb");
                    var src = resp.ReadAsStream();
                    Helpers.CopyToProgress(src,strm,(p)=>{
                        this.progress(p/2);
                    },100.0);
                    strm.Close();
                    src.Close();
                    this.tytd.Storage.MoveFile(path+".part",path);
                    break;
                }
                else {
                    var req = this.tytd.ManifestRequest(this.info.VideoId).playerResponse;
                    var width = 0;
                    var height = 0;
                    var sampleRate = 0;
                    var bitrate = 0;
                    var vurl = "";
                    var aurl = "";

                    each(var item : req.streamingData.adaptiveFormats)
                    {
                        if(TypeOf(item.height) == "Long")
                        {
                            if(item.width >= width && item.height >= height)
                            {
                                vurl = item.url;
                                width = item.width;
                                height = item.height;
                            }
                        }
                        else {
                            item.audioSampleRate = ParseLong(item.audioSampleRate);
                            if(item.audioSampleRate >= sampleRate && item.bitrate >= bitrate)
                            {
                                aurl = item.url;
                                sampleRate = item.audioSampleRate;
                                bitrate = item.bitrate;
                            }
                        }
                    }
        
                    this.video_stream_url = vurl;
                    this.audio_stream_url = aurl;
    
                }
            }
        }
        if(!this.tytd.Storage.FileExists(pathA))
        {
            for(var i = 0; i < 5; i++)
            {
                var req = {
                    FollowRedirects = true,
                    RequestHeaders = [
                            {
                                Key = "User-Agent",
                                
                                Value = "com.google.android.youtube/19.28.35 (Linux; U; Android 15; GB) gzip"
                            }
                    ]
                };
                var resp = Net.Http.MakeRequest(this.audio_stream_url,req);
        
                if(resp.StatusCode >= 200 && resp.StatusCode <= 299)
                {
                    this.tytd.Storage.CreateDirectory(pathA.GetParent());
                    var strm = this.tytd.Storage.OpenFile(pathA+".part","wb");
                    var src = resp.ReadAsStream();
                    Helpers.CopyToProgress(src,strm,(p)=>{
                        this.progress((p/2)+0.5);
                    },100.0);
                    strm.Close();
                    src.Close();
                    this.tytd.Storage.MoveFile(pathA+".part",pathA);
                    break;
                }
                else {
                    var req = this.tytd.ManifestRequest(this.info.VideoId).playerResponse;
                    var width = 0;
                    var height = 0;
                    var sampleRate = 0;
                    var bitrate = 0;
                    var vurl = "";
                    var aurl = "";

                    each(var item : req.streamingData.adaptiveFormats)
                    {
                        if(TypeOf(item.height) == "Long")
                        {
                            if(item.width >= width && item.height >= height)
                            {
                                vurl = item.url;
                                width = item.width;
                                height = item.height;
                            }
                        }
                        else {
                            item.audioSampleRate = ParseLong(item.audioSampleRate);
                            if(item.audioSampleRate >= sampleRate && item.bitrate >= bitrate)
                            {
                                aurl = item.url;
                                sampleRate = item.audioSampleRate;
                                bitrate = item.bitrate;
                            }
                        }
                    }
        
                    this.video_stream_url = vurl;
                    this.audio_stream_url = aurl;
    
                }
            }
        }
        this.progress(1.0);

        return;
    }
}