File size: 5,006 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MovieTube.Client.Scraper;
using System.Linq;

namespace MovieTube.Viewer
{
    public partial class HorizontalSingleRowGalleryWidget : GalleryWidgetBase
    {
        private int startIndex, endIndex;

        #region Constructor

        public HorizontalSingleRowGalleryWidget()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods


        protected override void OnAddItems(MoviePage page)
        {
            this.startIndex = 0;
            this.endIndex = Math.Min(page.Videos.Count - 1,GetControlEndIndex()) ;
            AddSinglePage();
        }

        protected override void OnAddItem(Movie movie, bool select)
        {
            var found = false;
            LogManager.Log("Adding items to Veritcal gallery");
            foreach (Control c in this.flowLayoutPanel.Controls)
            {
                var widget = c as WebViewWidget;
                if (widget != null)
                {
                    var link = widget.Video;
                    if (link.ID == movie.ID)
                    {
                        widget.SelectView();
                        found = true;
                    }
                    else
                        widget.DeselectView();
                }
            }
            if (!found)
            {
                var m = this.currentPage.Videos.FirstOrDefault(x => x.ID == movie.ID);
                if (m != null) 
                    this.currentPage.Videos.Remove(m);
                this.currentPage.Videos.Insert(this.startIndex, movie);
                AddSinglePage();
                if (select)
                    ((WebViewWidget)this.flowLayoutPanel.Controls[0]).SelectView();
             }
        }

        private WebViewWidget AddItem(Movie movie)
        {
            var widget = new WebViewWidget(movie,
                    Properties.Resources.TestHtml.Replace("{0}", movie.Url)
                    .Replace("{1}", String.IsNullOrEmpty(movie.ImageUrl) ? "no_image.gif" : movie.ImageUrl)
                    .Replace("{2}", movie.Name)
                    .Replace("{3}", movie.ReleaseDate.Year.ToString())
                    .Replace("{4}", movie.Description)
                    .Replace("{5}", movie.LanguageText));
            this.flowLayoutPanel.Controls.Add(widget);
            widget.ViewSelected += delegate(object sender, GalleryItemSelectedEventArgs e)
            {
                OnItemSelected(sender, e);
            };
            widget.Favourited += delegate(object sender, GalleryItemFavouriteEventArgs e)
            {
                OnItemFavourited(sender, e);
            };

            return widget;
        }

        private void AddSinglePage()
        {
            this.flowLayoutPanel.Controls.Clear();
            for (var i = this.startIndex; i <= this.endIndex; i++)
            {
                AddItem(this.currentPage.Videos[i]);
                if (Constants.ShowAds && i % 4 == 0)
                {
                    var ad = CreateAdWidget(Constants.HorizontalAdId);
                    this.flowLayoutPanel.Controls.Add(ad);
                }
            }
        }

        private int GetControlEndIndex()
        {
            if (this.Size.Width < 351)
                return 0;
            var i = this.Size.Width / 350 ;
            if (this.Size.Width % 350 > 100)
                return i;
            return i - 1;

        }

        protected override Control WidgetContainer
        {
            get
            {
                return this.flowLayoutPanel;
            }
        }

        public void ShowNext()
        {
            this.startIndex = this.endIndex;
            this.endIndex = Math.Min(this.startIndex  +GetControlEndIndex(), this.currentPage.Videos.Count - 1);
            AddSinglePage();
        }

        public void ShowPrevious()
        {
            this.startIndex = Math.Max(this.startIndex - GetControlEndIndex(), 0);
            this.endIndex = Math.Min(this.startIndex + GetControlEndIndex(), this.currentPage.Videos.Count - 1);
            AddSinglePage();
        }

        public bool IsEnd
        {
            get { return this.endIndex == this.currentPage.Videos.Count - 1; }
        }

        public bool IsBeginning { get { return this.startIndex == 0; } }
       
        #endregion

        private void HorizontalSingleRowGalleryWidget_Resize(object sender, EventArgs e)
        {
            if (this.currentPage != null)
            {
                this.endIndex =  Math.Min(this.startIndex + GetControlEndIndex(), this.currentPage.Videos.Count - 1);
                AddSinglePage();
            }
        }
    }
}