private void LoadData(Chapter chapter)
 {
     if (chapter == null)
     {
         throw new ArgumentNullException("chapter");
     }
     IsLoading      = true;
     Title          = chapter.Title;
     NextChapterId  = chapter.NextChapterId;
     PrevChapterId  = chapter.PrevChapterId;
     ParentVolumeId = chapter.ParentVolumeId;
     ParentSeriesId = chapter.ParentSeriesId;
     ChapterNo      = chapter.ChapterNo;
     Lines.Clear();
     foreach (var line in chapter.Lines)
     {
         if (line == null)
         {
             System.Diagnostics.Debug.WriteLine("null Lines in chapter data");
             continue;
         }
         var lv = LineViewModel.Create(line);
         if (lv != null)
         {
             Lines.Add(lv);
         }
     }
     CurrentLineNo = 1;
 }
        public async Task LoadCommentsAsync(LineViewModel lineView)
        {
            if (lineView.Comments.Count != 0)
            {
                return;
            }
            string lineId = lineView.Id.ToString();

            Debug.WriteLine("Loading Comments : line_id = " + lineId + " ,chapter_id = " + ChapterId);
            try
            {
                lineView.IsLoading = true;
                var comments = await LightKindomHtmlClient.GetCommentsAsync(lineId, ChapterId);

                foreach (var comment in comments)
                {
                    lineView.Comments.Add(new Comment(comment));
                }
                lineView.IsLoading = false;
            }
            catch (Exception)
            {
                Debug.WriteLine("Comments load failed : line_id = " + lineId + " ,chapter_id = " + ChapterId);
            }
        }
        public void ResetPhase0(LineViewModel line)
        {
            var iv = LayoutRoot;
            ImageContent.Opacity = 0;
            ImageContent.Height = double.NaN;
            CommentIndicator.Opacity = 0;
            ProgressBar.Opacity = 0;
            TextContent.Opacity = 1;
            if (ImageContent.Source != null)
            {
                var bitmap = ImageContent.Source as BitmapImage;
                bitmap.DownloadProgress -= Image_DownloadProgress;
                ImageContent.ClearValue(Image.SourceProperty);
            }

            if (line.IsImage)
            {
                if (!AppGlobal.ShouldAutoLoadImage)
                    TextContent.Text = ImageTapToLoadPlaceholder;
                else
                    TextContent.Text = ImageLoadingTextPlaceholder;

                double aspect = (double)line.ImageHeight / (double)line.ImageWidth;
                double ih = iv.Width * aspect;

                if (ih <= 1.0)
                    ih = 440;

                ImageContent.Height = ih;
                ImagePlaceHolder.Height = ih;

                ProgressBar.Visibility = Visibility.Visible;
                ImageContent.Visibility = Windows.UI.Xaml.Visibility.Visible;
                ImagePlaceHolder.Visibility = Windows.UI.Xaml.Visibility.Visible;
                TextContent.TextAlignment = TextAlignment.Center;
            }
            else
            {
                TextContent.Text = " " + line.Content;
                //textContent.Height = double.NaN;
                TextContent.TextAlignment = TextAlignment.Left;

                ImagePlaceHolder.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                ImageContent.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                ImageContent.DataContext = null;
            }
        }
        internal static LineViewModel Create(Line line)
        {
            LineViewModel lvm = null;

            if (line.ContentType == LineContentType.TextContent)
            {
                lvm = new LineViewModel(line.Content)
                {
                    Id = line.No
                }
            }
            ;
            else if (line.ContentType == LineContentType.ImageContent)
            {
                lvm = new IllustrationViewModel(line.Content)
                {
                    Id = line.No
                }
            }
            ;
            return(lvm);
        }
    }
        void SetUpComentFlyoutForLineView(IllustrationView iv, LineViewModel line)
        {
            var textContent = iv.FindName("TextContent") as TextBlock;
            if (line.IsImage && textContent.Text == ImageTapToLoadPlaceholder)
            {
                textContent.Text = ImageLoadingTextPlaceholder;
                iv.LoadIllustrationLine(line);
                //LoadItemIllustation(iv, line);
                return;
            }

            if (!(line.IsImage || ViewModel.EnableComments))
                return;

            ((FrameworkElement)CommentsFlyout.Content).DataContext = line;
            if (ViewModel.EnableComments)
            {
                CommentsTool.Visibility = Visibility.Visible;
                if (AppGlobal.IsSignedIn)
                {
                    CommentsInputTool.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
                else
                {
                    CommentsInputTool.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                }
            }
            else
            {
                CommentsTool.Visibility = Visibility.Collapsed;
            }

            if (line.IsImage && line.IsImageCached)
            {
                var imageContent = iv.FindName("ImageContent") as Image;
                var bitmap = imageContent.Source as BitmapImage;
                //if (bitmap.UriSource.AbsoluteUri.StartsWith("ms-appdata"))
                //{
                    ShowImageRefreshButton();
                //}
                //else
                //{
                //    HideImageRefreshButton();
                //}
            }
            else
            {
                HideImageRefreshButton();
            }
        }
		private void LoadItemIllustation(Grid iv, LineViewModel line)
		{
			var bitMap = new BitmapImage(line.ImageUri);
			var imageContent = iv.FindName("ImageContent") as Image;
			imageContent.DataContext = line;
			var imagePlaceHolder = iv.FindName("ImagePlaceHolder") as Windows.UI.Xaml.Shapes.Path;
			var progressIndicator = iv.FindName("ProgressBar") as ProgressBar;

			bitMap.SetValue(BitmapLoadingIndicatorProperty, progressIndicator);
			bitMap.DownloadProgress += Image_DownloadProgress;
			imageContent.ImageOpened += imageContent_ImageOpened;
			imageContent.ImageFailed += ImageContent_Failed;
			imageContent.Source = bitMap;
		}
		public async Task LoadCommentsAsync(LineViewModel lineView)
		{
			if (lineView.Comments.Count != 0)
				return;
			string lineId = lineView.Id.ToString();

			Debug.WriteLine("Loading Comments : line_id = " + lineId + " ,chapter_id = " + ChapterId);
			try
			{
				lineView.IsLoading = true;
				var comments = await LightKindomHtmlClient.GetCommentsAsync(lineId, ChapterId);
				foreach (var comment in comments)
				{
					lineView.Comments.Add(new Comment(comment));
				}
				lineView.IsLoading = false;
			}
			catch (Exception)
			{
				Debug.WriteLine("Comments load failed : line_id = " + lineId + " ,chapter_id = " + ChapterId);
			}
		}
		internal static LineViewModel Create(Line line)
		{
			LineViewModel lvm = null;
			if (line.ContentType == LineContentType.TextContent)
				lvm = new LineViewModel(line.Content) { Id = line.No };
			else if (line.ContentType == LineContentType.ImageContent)
				lvm = new IllustrationViewModel(line.Content) { Id = line.No };
			return lvm;
		}
		public async Task LoadIllustrationLine(LineViewModel line)
		{
			var bitMap = new BitmapImage();
			if (line.ImageHeight > 1.0)
			{
				bitMap.DecodePixelHeight = line.ImageHeight;
				bitMap.DecodePixelWidth = line.ImageWidth;
				bitMap.DecodePixelType = DecodePixelType.Physical;
			}

			ImageContent.DataContext = line;

			bitMap.SetValue(BitmapLoadingIndicatorProperty, ProgressBar);
			bitMap.DownloadProgress += Image_DownloadProgress;

			CommentIndicator.Opacity = line.HasComments ? 1 : 0;

			ImageContent.ImageOpened += ImageContent_ImageOpened;
			ImageContent.ImageFailed += ImageContent_Failed;

			var download = line.DownloadImageAsync();
            if (download.Status != AsyncStatus.Started)
            {
                download.Progress = (info, p) =>
                {
                    ProgressBar.Value = p;
                };
            }

			var stream = await download;
            var setTask = bitMap.SetSourceAsync(stream);
			ImageContent.Source = bitMap;
			await setTask;
		}