import template from './ws-footer.html';

FooterDirective.$inject = ['$translate'];

export default function FooterDirective($translate) {
  return {
    restrict: 'E',
    template,
    scope: {
      // Allow for two-way binding of this variable
      currentLanguage: '=lang',
      availableLanguages: '=langs',
    },
    link(scope) {
      const defaultLanguages = ['en_CA'];
      let availableLanguages = defaultLanguages;
      if (scope.availableLanguages) {
        availableLanguages = scope.availableLanguages.split(',');
      }

      // Only use lang if defined, fall back to the preferredLanguage above.
      if (scope.currentLanguage) {
        $translate.use(scope.currentLanguage);
      }

      scope.$watch('currentLanguage', (newLang) => {
        $translate.use(newLang);
      });

      scope.setLanguage = (code) => {
        scope.currentLanguage = code;
      };

      scope.nav = {
        currentYear: new Date().getFullYear(),
        contact: contact(),
        location: location(),
      };

      scope.languages = {};
      availableLanguages.forEach((code) => {
        $translate('general.language', {}, null, code, code).then((languageName) => {
          scope.languages[code] = languageName;
        });
      });
    },
  };
}

function contact() {
  return {
    addressLine1: '860 Richmond Street West, 3rd floor',
    addressLine2: 'Toronto, ON, M6J 1C9, Canada',
    supportEmail: 'support@wealthsimple.com',
  };
}

function location() {
  return {
    cipf: 'http://www.cipf.ca/public/cipfcoverage/coveragepolicy.aspx',
    facebook: 'https://www.facebook.com/wealthsimple',
    faqs: 'http://help.wealthsimple.com/hc/en-us',
    itunes: 'https://itunes.apple.com/ca/app/wealthsimple/id948087968?ls=1&mt=8',
    linkedin: 'https://www.linkedin.com/company/wealthsimple',
    play: 'https://play.google.com/store/apps/details?id=com.wealthsimple',
    twitter: 'https://twitter.com/wealthsimple',
    blog,
    sages,
  };
}

function blog(path) {
  return `http://grow.wealthsimple.com/${path}`;
}

function sages(path) {
  return `https://www.wealthsimple.com/${path}`;
}
