public ChallengeView(ChallengeData data, ChallengeProgress progress, int id)
 {
     Data = data;
     Progress = progress;
     progress.PropertyChanged += SubView_PropertyChanged;
     Id = id;
     if (data.Type == ChallengeType.Progressable)
     {
         subChallenges = new ObservableCollection<SubChallengeView>();
     }
 }
 private void fillWithExampleData()
 {
     ChallengeData cd = new ChallengeData("Kill Beyond Bosses", "Use the Beyond Map Mod or Zana Mod", ChallengeType.Progressable);
     SubChallengeInfo info = new SubChallengeInfo("poe.trade:", "");
     SubChallengeData scd = new SubChallengeData("Bameth");
     scd.Infos.Add(info);
     info = new SubChallengeInfo("Softcore", "http://poe.trade");
     scd.Infos.Add(info);
     info = new SubChallengeInfo("Hardcore", "http://google.de");
     scd.Infos.Add(info);
     SubChallengeData scd2 = new SubChallengeData("Abaxoth");
     cd.SubChallenges.Add(scd);
     cd.SubChallenges.Add(scd2);
 }
        internal void toogleChallengeIsDone(ChallengeData data, ChallengeProgress progress)
        {
            switch (data.Type)
            {
                case ChallengeType.Binary:
                    progress.IsDone = !progress.IsDone;
                    break;
                case ChallengeType.Progressable:
                    //Determine current progress
                    var currentProgress = 0;
                    foreach (var item in progress.SubChallengesProgress)
                    {
                        if (item.CurrentCompletion != SubChallengeCompletionType.Not)
                        {
                            currentProgress++;
                        }
                    }

                    if (currentProgress >= data.NeedForCompletion)
                    {
                        //Need to uncomplete subs
                        foreach (var item in progress.SubChallengesProgress.Where(x => x.CurrentCompletion == SubChallengeCompletionType.Auto))
                        {
                            item.CurrentCompletion = SubChallengeCompletionType.Not;
                        }
                    }
                    else
                    {
                        //Need to complete subs
                        foreach (var item in progress.SubChallengesProgress)
                        {
                            if (currentProgress < data.NeedForCompletion && item.CurrentCompletion == SubChallengeCompletionType.Not)
                            {
                                item.CurrentCompletion = SubChallengeCompletionType.Auto;
                                currentProgress++;
                            }
                        }
                    }
                    break;
            }
        }
        public LeagueInfo readXml(string xmlPath)
        {
            var uri = new Uri(xmlPath);
            var stream = new StreamReader(xmlPath).BaseStream;
            var leagueInfo = new LeagueInfo();
            var data = new List<ChallengeData>();
            leagueInfo.ChallengeDatas = data;
            XmlReader xml = XmlReader.Create(stream);
            bool insideLeaguename = false;
            bool insideStartedOn = false;
            bool insideEndsOn = false;
            bool insideSingleChallenge = false;
            bool insideSubChallenge = false;
            bool insideInfo = false;
            bool insideName = false;
            bool insideDescription = false;
            bool insideType = false;
            bool insideNeedForCompletion = false;
            bool insideNeedForCompletionSub = false;
            bool insideInfoText = false;
            bool insideInfoUrl = false;
            ChallengeData challengeData = null;
            SubChallengeData subChallengeData = null;
            SubChallengeInfo info = null;
            while (xml.Read())

            {
                switch (xml.NodeType)
                {
                    case XmlNodeType.Element:
                        string element = xml.Name;
                        if (element.Contains(":"))
                        {
                            element = element.Substring(element.IndexOf(":") + 1);
                        }
                        if (element == CHALLENGES)
                        {
                            continue;
                        }
                        if (!insideSingleChallenge && element == LEAGUENAME)
                        {
                            insideLeaguename = true;
                            continue;
                        }
                        if (!insideSingleChallenge && element == STARTED_ON)
                        {
                            insideStartedOn = true;
                            continue;
                        }
                        if (!insideEndsOn && element == ENDS_ON)
                        {
                            insideEndsOn = true;
                            continue;
                        }
                        if (element == CHALLENGE)
                        {
                            challengeData = new ChallengeData();
                            var attribute = xml.GetAttribute("createWikiLinks");
                            if (attribute != null)
                            {
                                challengeData.WikiLinksExist = attribute.ToLower() == "true";
                            }
                            insideSingleChallenge = true;
                            continue;
                        }
                        if (element == SUBCHALLENGE)
                        {
                            insideSubChallenge = true;
                            subChallengeData = new SubChallengeData();
                            continue;
                        }
                        if (insideSingleChallenge && !insideSubChallenge)
                        {
                            insideName = element == NAME;
                            insideDescription = element == DESCRIPTION;
                            insideType = element == TYPE;
                            insideNeedForCompletion = element == NEEDED_TO_COMPLETE;
                            continue;
                        }
                        if (insideSingleChallenge && insideSubChallenge && !insideInfo)
                        {
                            insideName = element == NAME;
                            insideNeedForCompletionSub = element == NEEDED_TO_COMPLETE_SUB;
                            if (element == INFO)
                            {
                                insideInfo = true;
                                info = new SubChallengeInfo();
                            }
                            continue;
                        }
                        if (insideSingleChallenge && insideSubChallenge && insideInfo)
                        {
                            insideInfoText = element == TEXT;
                            insideInfoUrl = element == URL;
                        }

                        break;
                    case XmlNodeType.Text:
                        if (insideLeaguename)
                        {
                            leagueInfo.Leaguename = xml.Value.Trim();
                            continue;
                        }
                        if (insideStartedOn)
                        {
                            string datetime = xml.Value.Trim();
                            if (datetime != null && datetime.Length > 0)
                            {

                                DateTime dt = DateTime.ParseExact(datetime, "yyyy-MM-dd HH:mmZ", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
                                leagueInfo.LeagueStartedOn = dt;
                            }
                            continue;
                        }
                        if (insideEndsOn)
                        {
                            string datetime = xml.Value.Trim();
                            if (datetime != null && datetime.Length > 0)
                            {
                                DateTime dt = DateTime.ParseExact(datetime, "yyyy-MM-dd HH:mmZ", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
                                leagueInfo.LeagueEndsOn = dt;
                            }
                            continue;
                        }
                        if (insideSingleChallenge && !insideSubChallenge)
                        {
                            if (insideName)
                            {
                                challengeData.Name = xml.Value.Trim();
                            }
                            if (insideDescription)
                            {
                                challengeData.Description = xml.Value.Trim();
                            }
                            if (insideType)
                            {
                                String type = xml.Value.Trim();
                                if (type == BINARY)
                                {
                                    challengeData.Type = ChallengeType.Binary;
                                    continue;
                                }
                                if (type == PROGRESSABLE)
                                {
                                    challengeData.Type = ChallengeType.Progressable;
                                    continue;
                                }
                                if (type == PROGRESSABLE_NO_SUBS)
                                {
                                    challengeData.Type = ChallengeType.ProgressableNoSubs;
                                    continue;
                                }
                                throw new InvalidOperationException("Encountered unknown challenge type during processing " + xmlPath);
                            }
                            if (insideNeedForCompletion)
                            {
                                String number = xml.Value.Trim();
                                challengeData.NeedForCompletion = Convert.ToInt32(number);
                            }
                        }
                        if (insideSingleChallenge && insideSubChallenge)
                        {
                            if (insideName)
                            {
                                var subName = xml.Value.Trim();
                                subChallengeData.Description = subName;
                                if (challengeData.WikiLinksExist)
                                {
                                    var generatedInfo = new SubChallengeInfo();
                                    generatedInfo.Text = "Wiki";
                                    generatedInfo.UrlAsString = WIKI_BASE_URL + subName.Replace(" ", "_");
                                    subChallengeData.Infos.Add(generatedInfo);
                                }
                            }
                            if (insideNeedForCompletionSub)
                            {
                                subChallengeData.IsProgressable = true;
                                subChallengeData.NeededToComplete = Convert.ToInt32(xml.Value.Trim());
                            }
                            if (insideInfo)
                            {
                                if (insideInfoText)
                                {
                                    info.Text = xml.Value.Trim();
                                }
                                if (insideInfoUrl)
                                {
                                    info.UrlAsString = xml.Value.Trim();
                                }
                            }
                        }
                        break;

                    case XmlNodeType.EndElement:
                        string endElement = xml.Name;
                        if (endElement.Contains(":"))
                        {
                            endElement = endElement.Substring(endElement.IndexOf(":") + 1);
                        }
                        if (!insideSingleChallenge && endElement == LEAGUENAME)
                        {
                            insideLeaguename = false;
                        }
                        if (!insideSingleChallenge && endElement == STARTED_ON)
                        {
                            insideStartedOn = false;
                        }
                        if (!insideSingleChallenge && endElement == ENDS_ON)
                        {
                            insideEndsOn = false;
                        }

                        if (endElement == CHALLENGE)
                        {
                            insideSingleChallenge = false;
                            data.Add(challengeData);
                        }

                        if (insideSingleChallenge && !insideSubChallenge)
                        {
                            insideName = insideDescription = insideType = insideNeedForCompletion = false;
                        }
                        if (insideSingleChallenge && insideSubChallenge && !insideInfo)
                        {
                            if (endElement == SUBCHALLENGE)
                            {
                                insideSubChallenge = false;
                                challengeData.SubChallenges.Add(subChallengeData);
                            }
                            if (endElement == NAME)
                            {
                                insideName = false;
                            }
                            if (endElement == NEEDED_TO_COMPLETE_SUB)
                            {
                                insideNeedForCompletionSub = false;
                            }
                        }
                        if (insideSingleChallenge && insideSubChallenge && insideInfo)
                        {
                            if (endElement == TEXT)
                            {
                                insideInfoText = false;
                            }
                            if (endElement == URL)
                            {
                                insideInfoUrl = false;
                            }
                            if (endElement == INFO)
                            {
                                insideInfo = false;
                                subChallengeData.Infos.Add(info);
                            }
                        }
                        break;
                }
            }
            stream.Dispose();
            xml.Dispose();
            return leagueInfo;
        }
        private static List<ChallengeData> parseUrlForChallengeData(String url)
        {
            //Get the entire challenge html page
            HtmlWeb htmlWeb = new HtmlWeb();
            HtmlDocument document = htmlWeb.Load(url);

            //Get the challenge list node
            var rootNode = document.DocumentNode.SelectSingleNode("//div[@class='achievement-list']");
            //Get the challenges nodes
            var challengesList = new List<ChallengeData>();
            var challengeNodes = rootNode.SelectNodes("div[@class='achievement clearfix'] | div[@class='achievement clearfix incomplete']");
            foreach (var node in challengeNodes)
            {
                //Grab the data
                var data = new ChallengeData();
                data.Name = node.SelectSingleNode("h2[1]").InnerHtml;
                data.Description = node.SelectSingleNode("div[@class='detail']/span[@class='text']").InnerHtml;

                //decide whether it has subchallenges
                var completionNode = node.SelectSingleNode("h2[2]");
                if (completionNode != null)
                {
                    //grab the subchallenge data

                    string completiontext = completionNode.InnerHtml.Substring(completionNode.InnerHtml.LastIndexOf('/') + 1);
                    data.NeedForCompletion = Convert.ToInt32(completiontext);
                    var subNodes = node.SelectNodes("div[@class='detail']/span[@class='items']/ul/li");
                    if (subNodes != null)
                    {
                        data.Type = ChallengeType.Progressable;
                        foreach (var subNode in subNodes)
                        {
                            var isProgressableSub = false;
                            var neededToComplete = 0;
                            var desc = subNode.InnerHtml.Trim();
                            var indexOfBracketOpen = checkIfProgressableSub(desc);
                            if (indexOfBracketOpen > 0)
                            {
                                isProgressableSub = true;
                                var indexOfSlash = desc.LastIndexOf("/");
                                var needed = desc.Substring(indexOfSlash + 1, desc.Length - indexOfSlash - 2);
                                if (needed.Contains(","))
                                {
                                    if (needed.Length == 5)
                                    {
                                        needed = needed.Remove(1, 1);
                                    }
                                }
                                neededToComplete = Convert.ToInt32(needed);
                                desc = desc.Substring(0, indexOfBracketOpen - 1).Trim();
                            }

                            var subdata = new SubChallengeData(desc);
                            subdata.IsProgressable = isProgressableSub;
                            subdata.NeededToComplete = neededToComplete;
                            data.SubChallenges.Add(subdata);
                        }
                    }
                    else
                    {
                        data.Type = ChallengeType.ProgressableNoSubs;
                    }
                }
                else
                {
                    data.Type = ChallengeType.Binary;
                }
                challengesList.Add(data);
            }
            return challengesList;
        }