/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import mediaLibrary from '@ohos.multimedia.medialibrary';
import featureAbility from '@ohos.ability.featureAbility'

import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index'

describe('favSmartAlbum.promise.test.js', function () {

    let mediaType = mediaLibrary.MediaType.IMAGE;
    let path = "Pictures/"
    var context = featureAbility.getContext();
    console.info('MediaLibraryTest : getMediaLibrary IN');
    var media = mediaLibrary.getMediaLibrary(context);
    console.info('MediaLibraryTest : getMediaLibrary OUT');
    var asset;
    var favSmartAlbum;
    beforeAll(function () {
        console.info('Smart Album Callback MediaLibraryTest: beforeAll： Prerequisites at the test suite level, which are executed before the test suite is executed.');
    })

    beforeEach(function () {
        console.info('Smart Album Callback MediaLibraryTest: beforeEach：Prerequisites at the test case level, which are executed before each test case is executed.');
    })
    afterEach(function () {
        console.info('Smart Album Callback MediaLibraryTest: afterEach： Test case-level clearance conditions, which are executed after each test case is executed.');
    })
    afterAll(function () {
        console.info('Smart Album Callback MediaLibraryTest: afterAll：  Test suite-level cleanup condition, which is executed after the test suite is executed');
    })
     
    /**
     * @tc.number    : SUB_MEDIA_MEDIALIBRARY_GETPRIVATEALBUM_CALLBACK_001
     * @tc.name      : Get PrivateSmartAlbum by fav
     * @tc.desc      : Get PrivateSmartAlbum by fav
     * @tc.size      : MEDIUM
     * @tc.type      : Function
     * @tc.level     : Level 0
     */

    it('SUB_MEDIA_MEDIALIBRARY_GETPRIVATEALBUM_CALLBACK_001', 0, async function (done) {
        try {
            asset = await media.createAsset(mediaType, "imagefavtest.jpg", path);
            media.getPrivateAlbum(mediaLibrary.PrivateAlbumType.TYPE_FAVORITE, getPrivateAlbumCallBack);
            done();
        } catch (error) {
            console.info('MediaLibraryTest : CALLBACK_001 getPrivateAlbum fail, message = ' + error);
            done();
        }
    });

    function getPrivateAlbumCallBack(err, favSmartAlbums) {
        if (favSmartAlbums != undefined) {
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK getPrivateAlbum favSmartAlbums');
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK favSmartAlbums ' + favSmartAlbums[0].albumName);
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK favSmartAlbums ' + favSmartAlbums[0].albumCapacity);
            favSmartAlbum = favSmartAlbums[0];
            favSmartAlbum.addAsset(asset.uri, addAssetCallBack);
            done();
        } else {
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK getPrivateAlbumCallBack Unsuccessfull ' + err);
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK getPrivateAlbumCallBack : FAIL');
            done();
        }
    }

    function addAssetCallBack(err, data) {
        if (data != undefined) {
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK fav addAsset success');
            favSmartAlbum.getFileAssets(getFileAssetsCallBack);
            favSmartAlbum.removeAsset(asset.uri, removeAssetCallBack);
            done();
        } else {
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK addAssetCallBack Unsuccessfull ' + err);
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK addAssetCallBack : FAIL');
            done();
        }
    }
    function removeAssetCallBack(err, data) {
        if (data != undefined) {
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK fav removeAsset success');
            favSmartAlbum.getFileAssets(getFileAssetsCallBack);
            done();
        } else {
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK removeAssetCallBack Unsuccessfull ' + err);
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK getPrivateremoveAssetCallBackAlbumCallBack : FAIL');
            done();
        }
    }
    function getFileAssetsCallBack(err, fSmartFetchFileResult) {
        if (fSmartFetchFileResult != undefined) {
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK getFileAssetsCallBack Successfull fSmartFetchFileResult = ' +
                         fSmartFetchFileResult.getCount());
            done();
        } else {
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK getFileAssetsCallBack Unsuccessfull ' + err);
            console.info('MediaLibraryTest : SMARTALBUM_CALLBACK getFileAssetsCallBack : FAIL');
            done();
        }
    }
})
