# This file is part of beets.
# Copyright 2015, Tom Jaspers.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
import os
from beets.library import Item

from test import _common
from test._common import unittest
from test.helper import TestHelper


class MetaSyncTest(_common.TestCase, TestHelper):
    itunes_library = os.path.join(_common.RSRC, 'itunes_library.xml')

    def setUp(self):
        self.setup_beets()
        self.load_plugins('metasync')

        self.config['metasync']['source'] = 'itunes'
        self.config['metasync']['itunes']['library'] = self.itunes_library

        self._set_up_data()

    def _set_up_data(self):
        items = [_common.item() for _ in range(2)]
        items[0].title = 'Tessellate'
        items[0].artist = 'alt-J'
        items[0].albumartist = 'alt-J'
        items[0].album = 'An Awesome Wave'
        items[0].itunes_rating = 60

        items[1].title = 'Breezeblocks'
        items[1].artist = 'alt-J'
        items[1].albumartist = 'alt-J'
        items[1].album = 'An Awesome Wave'

        for item in items:
            self.lib.add(item)

    def tearDown(self):
        self.unload_plugins()
        self.teardown_beets()

    def test_load_item_types(self):
        # This test also verifies that the MetaSources have loaded correctly
        self.assertIn('amarok_score', Item._types)
        self.assertIn('itunes_rating', Item._types)

    def test_pretend_sync_from_itunes(self):
        out = self.run_with_output('metasync', '-p')

        self.assertIn('itunes_rating: 60 -> 80', out)
        self.assertIn('itunes_rating: 100', out)
        self.assertIn('itunes_playcount: 31', out)
        self.assertIn('itunes_skipcount: 3', out)
        self.assertIn('itunes_lastplayed: 2015-05-04 12:20:51', out)
        self.assertIn('itunes_lastskipped: 2015-02-05 15:41:04', out)

        self.assertEqual(self.lib.items()[0].itunes_rating, 60)

    def test_sync_from_itunes(self):
        self.run_command('metasync')

        self.assertEqual(self.lib.items()[0].itunes_rating, 80)
        self.assertEqual(self.lib.items()[0].itunes_playcount, 0)
        self.assertEqual(self.lib.items()[0].itunes_skipcount, 3)
        self.assertFalse(hasattr(self.lib.items()[0], 'itunes_lastplayed'))
        self.assertEqual(self.lib.items()[0].itunes_lastskipped, 1423147264.0)

        self.assertEqual(self.lib.items()[1].itunes_rating, 100)
        self.assertEqual(self.lib.items()[1].itunes_playcount, 31)
        self.assertEqual(self.lib.items()[1].itunes_skipcount, 0)
        self.assertEqual(self.lib.items()[1].itunes_lastplayed, 1430734851.0)
        self.assertFalse(hasattr(self.lib.items()[1], 'itunes_lastskipped'))


def suite():
    return unittest.TestLoader().loadTestsFromName(__name__)

if __name__ == b'__main__':
    unittest.main(defaultTest='suite')

@@ -646,16 +646,58 @@
 port os%0A
+import time%0Afrom datetime import datetime%0A
 from bee
@@ -816,16 +816,115 @@
 elper%0A%0A%0A
+def _parsetime(s):%0A    return time.mktime(datetime.strptime(s, '%25Y-%25m-%25d %25H:%25M:%25S').timetuple())%0A%0A%0A
 class Me
@@ -2992,21 +2992,67 @@
 ped,
- 1423147264.0
+%0A                         _parsetime('2015-02-05 15:41:04')
 )%0A%0A 
@@ -3315,21 +3315,67 @@
 yed,
- 1430734851.0
+%0A                         _parsetime('2015-05-04 12:20:51')
 )%0A  

fix tests for Windows