File size: 8,458 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
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 Client.Scraper;


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

        private int widthLeftRight;
        private ScrapedVideo currentRequestedVideo;
        private ScrapedPage lastPage;

        #endregion

        #region Constrctor


        public ViewWindow()
        {
            InitializeComponent();
        }

        #endregion

        #region Events


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

            // Is the left header group currently expanded?
            if (kryptonSplitContainerMain.FixedPanel == FixedPanel.None)
            {
                // Make the left panel of the splitter fixed in size
                kryptonSplitContainerMain.FixedPanel = FixedPanel.Panel1;
                kryptonSplitContainerMain.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
                kryptonSplitContainerMain.Panel1MinSize = newWidth;
                kryptonSplitContainerMain.SplitterDistance = 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
                kryptonSplitContainerMain.FixedPanel = FixedPanel.None;
                kryptonSplitContainerMain.IsSplitterFixed = false;

                // Put back the minimise size to the original
                kryptonSplitContainerMain.Panel1MinSize = 100;

                // Calculate the correct splitter we want to put back
                kryptonSplitContainerMain.SplitterDistance = widthLeftRight;

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

            kryptonSplitContainerMain.ResumeLayout();
        }

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

        private void verticalSingleColumnGalleryWidget1_ItemSelected(object sender, GalleryItemSelectedEventArgs e)
        {
            LoadVideo(e.Video);
        }

        private void relatedVideoGalleryWidget_ItemSelected(object sender, GalleryItemSelectedEventArgs e)
        {
            LoadVideo(e.Video);
        }

        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.verticalSingleColumnGalleryWidget1.RefreshView();
        }

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

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

        #endregion

        #region Methods

        public void LoadVideo(ScrapedVideo video)
        {
            if (this.currentRequestedVideo == video && !video.FullyLoaded)
                return;
            Program.SetBusy();
            this.videoPlayerWidget.PlayStop();
            
            this.currentRequestedVideo = video;
            if (video.FullyLoaded)
                OnScrapVideoDetailsCompleted(video);
            else
                new ScraperService().ScrapVideosDetailsAsync(video, this);
            
        }

        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(ScrapedPage page)
        {
            LogManager.Log("Initializing view window");
            this.lastPage = page;
            this.buttonSpecHeaderGroupMore.Visible = GetNextPage() != null;
            this.verticalSingleColumnGalleryWidget1.AddItems(page);
        }

        public void OnScrapVideoCompleted(ScrapedPage page)
        {
            Program.SetIdle();
           this.InvokeEx(() =>
           {
               Initialize(page);
               
           });
            
        }

        public void OnScrapVideoDetailsCompleted(ScrapedVideo video)
        {
            this.InvokeEx(() =>
            {
                if (this.currentRequestedVideo == video)
                {

                    var played = this.videoPlayerWidget.PlayStart(video);

                    if (played)
                    {
                        this.verticalSingleColumnGalleryWidget1.AddItem(video, true);
                        this.relatedVideoGalleryWidget.ClearItems();
                        this.relatedVideoGalleryWidget.AddItems(new ScrapedPage { Videos = video.RelatedVideos });
                    }
                    else
                    {
                        this.currentRequestedVideo = null;
                    }
                }
                Program.SetIdle();
            });

        }


        public void OnScrapError(Exception ex)
        {
            Program.SetIdle();
            KryptonMessageBox.Show("An error occurred: " + ex.Message, Constants.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
           
        }

        public void ActivateView()
        {
            this.videoPlayerWidget.ActivateView();
            this.verticalSingleColumnGalleryWidget1.RefreshView();
            this.BringToFront();
            
        }

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

        public void TriggerSearch(SearchParameters param)
        {
            Program.SetBusy();
            this.verticalSingleColumnGalleryWidget1.ClearItems();
            new ScraperService().ScrapVideosAsync(this, param);
        }


        #endregion

      

       
    }
}