import { expect } from 'chai';
import { describe, it } from 'mocha';
import {
  avatarUrl,
  defaultAvatarUrl,
  displayAvatarUrl,
  emojiUrl,
  formatImageUrl,
  getWidgetImageUrl,
  guildBannerUrl,
  guildIconUrl,
  guildSplashUrl,
} from '../src/images.js';

describe('images.ts', () => {
  describe('formatImageUrl function', () => {
    it('will return formated url with default size 128 and webp', () => {
      expect(formatImageUrl('https://skillz.is.pro/image')).to.be.equal('https://skillz.is.pro/image.webp?size=128');
    });

    it('will return formated url with given size', () => {
      expect(formatImageUrl('https://skillz.is.pro/image', 1024)).to.be.equal('https://skillz.is.pro/image.webp?size=1024');
    });

    it('will return formated url with given size and format', () => {
      expect(formatImageUrl('https://skillz.is.pro/image', 1024, 'gif')).to.be.equal('https://skillz.is.pro/image.gif?size=1024');
    });

    it('will return formated url with default size and format', () => {
      expect(formatImageUrl('https://skillz.is.pro/image', undefined, 'gif')).to.be.equal('https://skillz.is.pro/image.gif?size=128');
    });

    describe('without format', () => {
      it('will use gif if a_ is found', () => {
        expect(formatImageUrl('https://cdn.discordapp.com/avatars/568505543511259840/a_482491d6dcf12e12746ccd3148f0c646')).to.be.equal(
          'https://cdn.discordapp.com/avatars/568505543511259840/a_482491d6dcf12e12746ccd3148f0c646.gif?size=128',
        );
      });

      it('will use webp if no a_ is found', () => {
        expect(formatImageUrl('https://cdn.discordapp.com/avatars/568505543511259840/482491d6dcf12e12746ccd3148f0c646')).to.be.equal(
          'https://cdn.discordapp.com/avatars/568505543511259840/482491d6dcf12e12746ccd3148f0c646.webp?size=128',
        );
      });
    });
  });

  describe('emojiUrl function', () => {
    it('can format emoji url with png as default ext', () => {
      expect(emojiUrl('1079823706743918622')).to.equal('https://cdn.discordapp.com/emojis/1079823706743918622.png');
    });

    it('can format emoji url with gif as ext', () => {
      expect(emojiUrl('1079584570661404724', true)).to.equal('https://cdn.discordapp.com/emojis/1079584570661404724.gif');
    });

    it('can format emoji url with webp as ext', () => {
      expect(emojiUrl('1079823706743918622', false, 'webp')).to.equal('https://cdn.discordapp.com/emojis/1079823706743918622.webp');
    });

    it('can format emoji url with webp as ext when animated', () => {
      expect(emojiUrl('1079823706743918622', true, 'webp')).to.equal('https://cdn.discordapp.com/emojis/1079823706743918622.webp?animated=true');
    });
  });

  describe('avatarUrl function', () => {
    it('will return the url for given avatar icon hash', () => {
      expect(avatarUrl('207324334904049664', 'db26a6fb924c985f66b79364cf5797b7')).to.equal(
        'https://cdn.discordapp.com/avatars/207324334904049664/db26a6fb924c985f66b79364cf5797b7.webp?size=128',
      );
    });

    it('will return the url for given avatar icon bigint', () => {
      expect(avatarUrl('207324334904049664', 4034407661299384404326332419647968090039n)).to.equal(
        'https://cdn.discordapp.com/avatars/207324334904049664/db26a6fb924c985f66b79364cf5797b7.webp?size=128',
      );
    });
  });

  describe('defaultAvatarUrl function', () => {
    it('will return the url for default avatar', () => {
      expect(defaultAvatarUrl('207324334904049664', '9130')).to.equal('https://cdn.discordapp.com/embed/avatars/0.png');
    });
  });

  describe('displayAvatarUrl function', () => {
    it('will return the url for given avatar icon hash', () => {
      expect(displayAvatarUrl('207324334904049664', '9130', 'db26a6fb924c985f66b79364cf5797b7')).to.equal(
        'https://cdn.discordapp.com/avatars/207324334904049664/db26a6fb924c985f66b79364cf5797b7.webp?size=128',
      );
    });

    it('will return the url for given avatar icon bigint', () => {
      expect(displayAvatarUrl('207324334904049664', '9130', 4034407661299384404326332419647968090039n)).to.equal(
        'https://cdn.discordapp.com/avatars/207324334904049664/db26a6fb924c985f66b79364cf5797b7.webp?size=128',
      );
    });

    it('will return the url for default avatar', () => {
      expect(displayAvatarUrl('207324334904049664', '9130', undefined)).to.equal('https://cdn.discordapp.com/embed/avatars/0.png');
    });
  });

  describe('guildBannerUrl function', () => {
    it("will return the url for given guild's banner's icon hash", () => {
      expect(guildBannerUrl('785384884197392384', { banner: '2fc0f64acd7a326e0c93c123db02eb1d' })).to.equal(
        'https://cdn.discordapp.com/banners/785384884197392384/2fc0f64acd7a326e0c93c123db02eb1d.webp?size=128',
      );
    });

    it("will return the url for given guild's banner's icon big int", () => {
      expect(guildBannerUrl('785384884197392384', { banner: 3806581668328291509506503737571885116189n })).to.equal(
        'https://cdn.discordapp.com/banners/785384884197392384/2fc0f64acd7a326e0c93c123db02eb1d.webp?size=128',
      );
    });

    it("will return the url for given guild's banner with format", () => {
      expect(guildBannerUrl('785384884197392384', { banner: '2fc0f64acd7a326e0c93c123db02eb1d', format: 'png' })).to.equal(
        'https://cdn.discordapp.com/banners/785384884197392384/2fc0f64acd7a326e0c93c123db02eb1d.png?size=128',
      );
    });

    it("will return the url for given guild's banner with size", () => {
      expect(guildBannerUrl('785384884197392384', { banner: '2fc0f64acd7a326e0c93c123db02eb1d', size: 256 })).to.equal(
        'https://cdn.discordapp.com/banners/785384884197392384/2fc0f64acd7a326e0c93c123db02eb1d.webp?size=256',
      );
    });

    it('will return undefined without given banner', () => {
      expect(guildBannerUrl('785384884197392384', {})).to.equal(undefined);
    });
  });

  describe('guildIconUrl function', () => {
    it("will return the url for given guild's icon's icon hash", () => {
      expect(guildIconUrl('785384884197392384', '7cb67c989d54d824239b2bb4270955b1')).to.equal(
        'https://cdn.discordapp.com/icons/785384884197392384/7cb67c989d54d824239b2bb4270955b1.webp?size=128',
      );
    });

    it("will return the url for given guild's icon's icon big int", () => {
      expect(guildIconUrl('785384884197392384', 3908877832746069276949504774836813649329n)).to.equal(
        'https://cdn.discordapp.com/icons/785384884197392384/7cb67c989d54d824239b2bb4270955b1.webp?size=128',
      );
    });

    it("will return the url for given guild's icon with format", () => {
      expect(guildIconUrl('785384884197392384', '7cb67c989d54d824239b2bb4270955b1', { format: 'png' })).to.equal(
        'https://cdn.discordapp.com/icons/785384884197392384/7cb67c989d54d824239b2bb4270955b1.png?size=128',
      );
    });

    it("will return the url for given guild's icon with size", () => {
      expect(guildIconUrl('785384884197392384', '7cb67c989d54d824239b2bb4270955b1', { size: 256 })).to.equal(
        'https://cdn.discordapp.com/icons/785384884197392384/7cb67c989d54d824239b2bb4270955b1.webp?size=256',
      );
    });

    it('will return undefined without given icon', () => {
      expect(guildIconUrl('785384884197392384', undefined)).to.equal(undefined);
    });
  });

  describe('guildSplashUrl function', () => {
    it("will return the url for given guild's splash's icon big hash", () => {
      expect(guildSplashUrl('785384884197392384', '207961ff6c41f119874e10efc602858c')).to.equal(
        'https://cdn.discordapp.com/splashes/785384884197392384/207961ff6c41f119874e10efc602858c.webp?size=128',
      );
    });

    it("will return the url for given guild's splash's icon big int", () => {
      expect(guildSplashUrl('785384884197392384', 3786271587545740215322752847582515594636n)).to.equal(
        'https://cdn.discordapp.com/splashes/785384884197392384/207961ff6c41f119874e10efc602858c.webp?size=128',
      );
    });

    it("will return the url for given guild's splash with format", () => {
      expect(guildSplashUrl('785384884197392384', '207961ff6c41f119874e10efc602858c', { format: 'png' })).to.equal(
        'https://cdn.discordapp.com/splashes/785384884197392384/207961ff6c41f119874e10efc602858c.png?size=128',
      );
    });

    it("will return the url for given guild's splash with size", () => {
      expect(guildSplashUrl('785384884197392384', '207961ff6c41f119874e10efc602858c', { size: 2048 })).to.equal(
        'https://cdn.discordapp.com/splashes/785384884197392384/207961ff6c41f119874e10efc602858c.webp?size=2048',
      );
    });

    it('will return undefined without given icon', () => {
      expect(guildSplashUrl('785384884197392384', undefined)).to.equal(undefined);
    });
  });

  describe('getWidgetImageUrl function', () => {
    it("will return the url for given guild's widget", () => {
      expect(getWidgetImageUrl('785384884197392384')).to.equal('https://discordapp.com/api/guilds/785384884197392384/widget.png');
    });

    it("will return the url for given guild's widget with the style", () => {
      expect(getWidgetImageUrl('785384884197392384', { style: 'banner2' })).to.equal(
        'https://discordapp.com/api/guilds/785384884197392384/widget.png?style=banner2',
      );
    });
  });
});
