File size: 11,367 Bytes
e26fba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using ComponentFactory.Krypton.Toolkit;
using MovieTube.Client.Scraper;
using MovieTube.Viewer.Data;
using System.Diagnostics;
using System.IO;
using System.Net;


namespace MovieTube.Viewer
{
    public partial class ViewWindow : UserControl, IScraperServiceCallback, IView, IDownloadProgress
    {
        #region Fields

        private int widthLeftRight;
        private MovieLink currentRequestedVideo;
        private MoviePage lastPage;
        private IPlayerWidget playerWidget;
        public static MovieDownloadForm DownloadForm = new MovieDownloadForm();

        #endregion

        #region Constrctor


        public ViewWindow()
        {
            InitializeComponent();
            this.playerWidget = this.wmpVideoPlayerWidget;   
        }

        #endregion

        #region Events


        private void buttonSpecHeaderGroupSearchResult_Click(object sender, EventArgs e)
        {
            // Suspend layout changes until all splitter properties have been updated
            kryptonSplitContainerTop.SuspendLayout();

            // Is the left header group currently expanded?
            if (kryptonSplitContainerTop.FixedPanel == FixedPanel.None)
            {
                // Make the left panel of the splitter fixed in size
                kryptonSplitContainerTop.FixedPanel = FixedPanel.Panel2;
                kryptonSplitContainerTop.IsSplitterFixed = true;

                // Remember the current height of the header group
                widthLeftRight = headerGroupSearchResult.Width;

                // We have not changed the orientation of the header yet, so the width of 
                // the splitter panel is going to be the height of the collapsed header group
                int newWidth = headerGroupSearchResult.PreferredSize.Height;

                // Make the header group fixed just as the new height
                kryptonSplitContainerTop.Panel2MinSize = newWidth;
                kryptonSplitContainerTop.SplitterDistance = kryptonSplitContainerTop.Width- newWidth;

                // Change header to be vertical and button to near edge
                headerGroupSearchResult.HeaderPositionPrimary = VisualOrientation.Right;
                headerGroupSearchResult.ButtonSpecs[0].Edge = PaletteRelativeEdgeAlign.Near;
            }
            else
            {
                // Make the bottom panel not-fixed in size anymore
                kryptonSplitContainerTop.FixedPanel = FixedPanel.None;
                kryptonSplitContainerTop.IsSplitterFixed = false;

                // Put back the minimise size to the original
                kryptonSplitContainerTop.Panel2MinSize = 100;

                // Calculate the correct splitter we want to put back
                kryptonSplitContainerTop.SplitterDistance = kryptonSplitContainerTop.Width - widthLeftRight;

                // Change header to be horizontal and button to far edge
                headerGroupSearchResult.HeaderPositionPrimary = VisualOrientation.Top;
                headerGroupSearchResult.ButtonSpecs[0].Edge = PaletteRelativeEdgeAlign.Far;
            }

            kryptonSplitContainerTop.ResumeLayout();
        }

        private void buttonSpecHeaderGroupCollapse_Click(object sender, EventArgs e)
        {
            this.tableLayoutPanel1.SuspendLayout();
            kryptonHeaderGroup.Height = kryptonHeaderGroup.Height == kryptonHeaderGroup.PreferredSize.Height ?
                this.videoThumbGalleryWidget.MinimumSize.Height +
                kryptonHeaderGroup.PreferredSize.Height
                : kryptonHeaderGroup.PreferredSize.Height;
            this.tableLayoutPanel1.ResumeLayout();
        }

        private void buttonSpecHeaderGroupNext_Click(object sender, EventArgs e)
        {
            this.videoThumbGalleryWidget.ShowNext();
            UpdateNavigation();
        }

        private void buttonSpecHeaderGroupPrevious_Click(object sender, EventArgs e)
        {
            this.videoThumbGalleryWidget.ShowPrevious();
            UpdateNavigation();
        }


        private void verticalSingleColumnGalleryWidget1_ItemSelected(object sender, GalleryItemSelectedEventArgs e)
        {
            LoadVideo(e.Data as MovieLink);
        }

        private void videoLinksGalleryWidget_DownloadClicked(object sender, GalleryItemSelectedEventArgs e)
        {
            if (!Directory.Exists(AppSettings.MovieDownloadFolder))
            {
                using (var di = new FolderBrowserDialog() { Description = "Select download folder" })
                {
                    if (di.ShowDialog() == DialogResult.OK)
                        AppSettings.MovieDownloadFolder = di.SelectedPath;
                    else
                        return;
                }
            }
            {
                var link = (MovieLink)e.Data;
                var ds = new DownloaderService();
                var di = new DownloadInfo
                {
                    Link = link,
                    Service = ds
                };
                
                ds.Download(di, this);
                DownloadForm.ShowMe();
                DownloadForm.Add(di, 0);
            }
        }

        private void relatedVideoGalleryWidget_ItemSelected(object sender, GalleryItemSelectedEventArgs e)
        {
            LoadVideo(e.Data as Movie);
        }

        private void buttonSpecHeaderGroupMore_Click(object sender, EventArgs e)
        {
            var page = GetNextPage();
            if (page!= null)
            {
                //new ScraperService().ScrapVideosAsync(this, new SearchParameters { Url = page.Url });
            }
        }

        private void videoPlayerWidget_ToggelFavourite(object sender, ToggleFavouriteEventArgs e)
        {
            this.videoThumbGalleryWidget.RefreshView();
        }

        private void videoPlayerWidget_PlayNext(object sender, PlayEventArgs e)
        {
            var next = this.videoThumbGalleryWidget.GetNextVideo(e.Current);
            if (next != null)
                this.videoThumbGalleryWidget.SelecteItem(next);
        }

        private void videoPlayerWidget_PlayPrevious(object sender, PlayEventArgs e)
        {
            var pevious = this.videoThumbGalleryWidget.GetPreviousVideo(e.Current);
            if (pevious != null)
                this.videoThumbGalleryWidget.SelecteItem(pevious);
        }

        #endregion

        #region Methods


        public void LoadVideo(MovieLink video)
        {
            this.playerWidget.PlayStop();
            this.playerWidget.ShowBuffering();
            this.currentRequestedVideo = video;
            if (video.ScrapState == LinkScrapState.FullyLoaded)
                OnScrapVideoDetailsCompleted(video);
            else if (video.ScrapState == LinkScrapState.VideoDoesNotExists)
                OnScrapError(video, "Video is removed");
            else
                new ScraperService().ScrapVideosDetailsAsync(this, video);
        }

        public void LoadVideo(Movie movie, bool playFirstLink = false)
        {
            var s = ClientDataService.Single.Settings;
            s.PlayFirstLink = playFirstLink ? true : s.PlayFirstLink;
            this.videoThumbGalleryWidget.AddItem(movie, true);
            this.videoLinksGalleryWidget.ClearItems();
            this.videoLinksGalleryWidget.AddItem(movie, s.PlayFirstLink);
            new ScraperService().ScrapVideosDetailsAsync(this, movie);

            this.playerWidget.PlayStop();

            if (s.PlayFirstLink)
                LoadVideo(movie.Links[0]);
        }

        private PagingLink GetNextPage()
        {
            if (this.lastPage != null && this.lastPage.Links.Count > 0)
            {
                if (this.lastPage.Links.Count == 1 && !this.lastPage.Links[0].IsSelected)
                    return this.lastPage.Links[0];
                var index = this.lastPage.Links.IndexOf(this.lastPage.Links.FirstOrDefault(x => x.IsSelected));
                if ( this.lastPage.Links.Count > index + 1)
                    return this.lastPage.Links[index + 1];
            }
             return null;

        }
       
        public void Initialize(MoviePage page)
        {
            LogManager.Log("Initializing view window");
            this.lastPage = page;
            this.buttonSpecHeaderGroupMore.Visible = GetNextPage() != null;
            this.videoThumbGalleryWidget.ClearItems();
            this.videoThumbGalleryWidget.AddItems(page);
            UpdateNavigation();
        }

        public void ActivateView()
        {
            this.playerWidget.ActivateView();
            this.videoLinksGalleryWidget.RefreshView();
            this.videoThumbGalleryWidget.RefreshView();
            this.BringToFront();
            
        }

        public void DeactivateView()
        {
            this.playerWidget.DeactivateView();
        }

        public void StopPlay()
        {
            this.playerWidget.PlayStop();
        }

        public void TriggerSearch(SearchParameters param)
        {
            Program.SetBusy();
            Initialize(ClientDataService.Single.GetAllMovies(param));
            Program.SetIdle();
        }

        private void UpdateNavigation()
        {
            this.buttonSpecHeaderGroupNext.Enabled = !this.videoThumbGalleryWidget.IsEnd ? ButtonEnabled.True : ButtonEnabled.False;
            this.buttonSpecHeaderGroupPrevious.Enabled = !this.videoThumbGalleryWidget.IsBeginning ? ButtonEnabled.True : ButtonEnabled.False;
        }


        #endregion

        #region IScraperServiceCallback

        public void OnScrapVideoDetailsCompleted(MovieLink link)
        {
            this.InvokeEx(() =>
            {
                if (this.currentRequestedVideo == link)
                {
                    Tracer.WriteLine(String.Format("Playing {0}, Link: {1}",  link.PlayUrl, link.DowloadUrl));
                    this.playerWidget.PlayStart(link);
                }
                this.playerWidget.HideBuffering();
            });

        }
      

        public void OnScrapError(MovieLink link, string message)
        {
            this.InvokeEx(() =>
            {
                this.playerWidget.HideBuffering();
                if (this.currentRequestedVideo == link)
                    KryptonMessageBox.Show(this, message, Constants.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            });

        }

        #endregion

        #region IDownloadProgress

        public void OnProgress(DownloadInfo di, DownloadProgressChangedEventArgs e)
        {
            DownloadForm.UpdateProgress(di, e);
        }

        public void OnCompletion(DownloadInfo di,AsyncCompletedEventArgs e)
        {
            DownloadForm.UpdateCompletion(di,e);
        }

      
        #endregion

       
    }
}