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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Diagnostics;
using System.Threading;
using MovieTube.Client.Scraper;
using MovieTube.Viewer.Data;
using System.Linq;

namespace MovieTube.Viewer
{
    partial class MainForm : KryptonForm,IViewContainer
    {
        #region Fields

        private IView currentView;
        private Thread downloaderThread;
        private UpdaterService updaterService = new UpdaterService();
        private NotepadForm fakeView;

        #endregion

        #region Constructor

        public MainForm()
        {
            InitializeComponent();
            this.currentView = this.viewerWindow;
            this.Text = Constants.AppTitle;
            System.Net.ServicePointManager.Expect100Continue = false;
            // this.fakeView = new NotepadForm(this);
        }

        #endregion

        #region Methods

       
        private void ChangeView(IView futureView)
        {
            
            ChangeViewSingle(this.headerView, futureView == this.viewerWindow);
            ChangeViewSingle(this.headerBrowse, futureView == this.browseWindow);
            ChangeViewSingle(this.headerFavourite, futureView == this.favouriteWindow);
           
            currentView.DeactivateView();
            futureView.ActivateView();
            currentView = futureView;
        }

        private void ChangeViewSingle(KryptonHeader view, bool selected)
        {
            if (!selected)
            {
                view.StateNormal.Back.Color1 = System.Drawing.SystemColors.GrayText;
                view.StateNormal.Back.Color2 = System.Drawing.SystemColors.InactiveCaption;
                view.StateNormal.Content.ShortText.Color1 = System.Drawing.SystemColors.GradientInactiveCaption;
                view.StateNormal.Content.ShortText.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            }
            else
            {
                view.StateNormal.Back.Color1 = System.Drawing.SystemColors.InactiveCaptionText;
                view.StateNormal.Back.Color2 = System.Drawing.SystemColors.InactiveCaption;
                view.StateNormal.Content.ShortText.Color1 = Color.White;
                view.StateNormal.Content.ShortText.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            }
        }


        public void PlayVideo(Movie video)
        {
            ChangeView(this.viewerWindow);
            this.viewerWindow.LoadVideo(video);
        }

        public IView ActiveView
        {
            get { throw new NotImplementedException(); }
            private set { }
        }
        
        

        private void ReloadApp(Version version)
        {
            this.InvokeEx(() =>
            {
                KryptonMessageBox.Show(this, String.Format("A new version ({0}) of the application is downloaded. The application will be closed while installing", version),
                    Constants.AppTitle);
                if(new UpdaterService().ExecuteUpdater())
                    Environment.Exit(0);
            });
        }

        private void CheckMovieDBUpdate(bool showMsg)
        {
            if (updaterService.DownloadMovieDatabase(true))
                ReloadDB();
            else if (showMsg)
                KryptonMessageBox.Show(this, "No new movies found", Constants.AppTitle);
        }

        private void ReloadDB()
        {
            this.InvokeEx(() =>
            {
                var movies = ClientDataService.Single.GetLatestMovies();
                if (movies.Count > 0)
                {
                    new DBUpdatedForm().ShowMe(movies);
                    if (this.currentView == this.viewerWindow)
                    {
                        var lang = this.searchPanelWidget.GetLanguage();
                        if (movies.Any(x => x.ModifiedDate == null && x.LanguageCode == lang))
                        {
                            this.currentView.TriggerSearch(new SearchParameters
                            {
                                Language = this.searchPanelWidget.GetLanguage(),
                                Year = 0
                            });
                        }
                    }
                }
               
                UpdateStatusBar();
            });
        }

        private void UpdateStatusBar(string message = "")
        {
            //if (String.IsNullOrEmpty(message))
            //    this.toolStripStatusLabel1.Text = "Total Movies: " + this.movieListWidget.Movies.Count;
            //else
            //    this.toolStripStatusLabel1.Text = message;
        }

        private void ShowApp()
        {
            this.Show();
            this.WindowState = FormWindowState.Maximized;
            this.BringToFront();
            bool top = TopMost;
            // make our form jump to the top of everything
            TopMost = true;
            // set it back to whatever it was
            TopMost = top;
        }

        private void StartUpdateCheckThread(bool forcefully)
        {
            this.toolStripStatusLabelMessage.Text = Constants.ServerMessage;
            if (downloaderThread == null)
            {
                downloaderThread = new Thread(DownloadWorker);
                downloaderThread.IsBackground = true;
                downloaderThread.Start(forcefully);
            }
        }

        private void DownloadWorker(object data)
        {

            var forcefull = (bool)data;
            CheckMovieDBUpdate(false);
            var v = updaterService.DownloadAppUpdate(forcefull);
            if(v > Constants.AppVersion)
                ReloadApp(v);
            downloaderThread = null;
            if (!forcefull)
            {
                Constants.UpdateShowAds();
                Constants.UpdateMessage();
            }
        }

        #endregion

        #region Events


        private void openToolStripMenuItemOpenNStream_Click(object sender, EventArgs e)
        {
            var frm = new NetworkStreamForm();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    var scraper = VideoScraperBase.GetScraper(frm.Url);
                    var movie = new Movie
                    {
                        Description = frm.Url,
                        Url = frm.Url,
                        Name = frm.Url,
                        ReleaseDate  =DateTime.Now
                    };
                    movie.Links.Add(new MovieLink
                    {
                        DowloadUrl = frm.Url,
                        Parent = movie,
                        LinkTitle = frm.Url,
                        PageSiteID = scraper.Title,
                        DownloadSiteID = scraper.Title,
                    });
                    this.viewerWindow.LoadVideo(movie, true);
                }
                catch(Exception ex) {
                    KryptonMessageBox.Show(this, ex.Message,
                        Constants.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

      
        private void toolStripButtonShowNew_Click(object sender, EventArgs e)
        {
            var lang = ((ToolStripItem)sender).Tag != null ?  ((ToolStripItem)sender).Tag.ToString() : null;
            this.currentView.TriggerSearch(new SearchParameters { Query = "_new", Language = lang });
        }

       
        private void downloadsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ViewWindow.DownloadForm.ShowMe();
        }

        private void custom2_Click(object sender, EventArgs e)
        {
            IView futureView = null;
            if (sender == this.headerView)
                futureView = this.viewerWindow;
            else if (sender == this.headerBrowse)
                futureView = this.browseWindow;
            else if (sender == this.headerFavourite)
                futureView = this.favouriteWindow;

            if (currentView == futureView)
                return;

            ChangeView(futureView);

        }

        private void searchPanelWidget_Search(object sender, SearchEventArgs e)
        {
            this.currentView.TriggerSearch(e.Params);
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QuitApp();
        }

        private void playToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            // var activeView = this.currentView == this.viewerWindow;

        }

        private void gotoWebsiteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start(UrlConstants.WebsiteUrl);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AboutBox().ShowDialog();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
            {
                LogManager.Log("Starting application");
                var movies = ClientDataService.Single.GetAllMovies(new SearchParameters() {
                    Language = ClientDataService.Single.Settings.DefaultLanguage
                });
                this.viewerWindow.Initialize(movies);
                //this.browseWindow.Initialize(movies);
            }
            this.viewerWindow.Focus();
            StartUpdateCheckThread(true);
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.S)       // Ctrl-S Save
            {
                if (!this.fakeView.Visible)
                {
                    this.Hide();
                    this.fakeView.ShowDialog();
                }
                else
                {
                    this.Show();
                    this.fakeView.Hide();
                }
                e.SuppressKeyPress = true;
            }
        }

        private void reportIssueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            Process.Start(UrlConstants.ReportIssueUrl);
        }

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Process.Start(UrlConstants.HelpUrl);
        }

        public void OnScrapVideoDetailsCompleted(MovieLink link)
        {
            throw new NotImplementedException();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Options().ShowDialog(this);
        }

        private void timerDB_Tick(object sender, EventArgs e)
        {
            StartUpdateCheckThread(false);
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            CheckMovieDBUpdate(true);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowApp();
        }

        private void gotoWebsiteToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Process.Start(UrlConstants.WebsiteUrl);
        }

        private void checkForNewMoviesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CheckMovieDBUpdate(true);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QuitApp();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.viewerWindow.StopPlay();
            this.Hide();
            e.Cancel = true;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                this.Hide();
            }
        }

        private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
                ShowApp();

        }
        TraceForm traceForm;
        private void traceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (traceForm == null || traceForm.IsDisposed)
                traceForm = new TraceForm();
            traceForm.Show();
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == NativeMethods.WM_SHOWME)
                ShowApp();
            base.WndProc(ref m);
        }

        private void QuitApp()
        {
            this.FormClosing -= new FormClosingEventHandler(MainForm_FormClosing);
            ViewWindow.DownloadForm.Dispose();
            Application.Exit();
        }

        private void toolStripButtonShare_Click(object sender, EventArgs e)
        {
            Process.Start("https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fwww.filmkhoj.com");
        }

        #endregion

    
     
    }
}