'use strict';

const moment = require('moment');
const _ = require('lodash');
const request = require('../lib/request');

module.exports = {
  leagueName: 'PGA',
  urls: function()  {
    return request.get('http://www.pgatour.com/data/r/current/schedule.json').then((res) => {
      const schedule = JSON.parse(res);
      const now = moment();
      const tour = _.find(schedule.tours, {desc: 'PGA TOUR'});
      if (!tour) {
        return Promise.resolve([]);
      }
      const current = _.find(tour.trns, (t) => {
        return  now > moment(t.date.start) && now < moment(t.date.end).add(1, 'd') &&
                t.FedExCup === 'Yes' &&
                t.primaryEvent === 'Y';
      });
      if (!current) {
        return Promise.resolve([]);
      }
      const url = `http://www.pgatour.com/data/r/${current.permNum}/leaderboard-v2.json`;
      return Promise.resolve([url]);
    });
  },
  Scores: require('../models/pga/Scores'),
  occasionalFetch: true,
  twitterName: 'PGATweetZone',
  seqTweet: true
};

@@ -201,30 +201,36 @@
 st.get('http
+s
 ://
-www
+statdata
 .pgatour.com
@@ -230,21 +230,16 @@
 our.com/
-data/
 r/curren
@@ -248,16 +248,19 @@
 schedule
+-v2
 .json').
@@ -331,23 +331,194 @@
 nst 
-now = moment();
+weekNumber = schedule.thisWeek.weekNumber;%0A      const year = _.find(schedule.years, %7B year: moment().year().toString() %7D);%0A      if (!year) %7B%0A        return Promise.resolve(%5B%5D);%0A      %7D
 %0A   
@@ -540,24 +540,20 @@
  _.find(
-schedule
+year
 .tours, 
@@ -705,74 +705,40 @@
 rn  
-now %3E moment(t.date.start) && now %3C moment(t.date.end).add(1, 'd')
+t.date.weekNumber === weekNumber
  &&%0A

update schedule URLs