/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * 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.
 */

#include <map>
#include <utility>
#include <vector>

#define LOG_TAG "VtsHalHapticGeneratorTargetTest"
#include <android-base/logging.h>
#include <android/binder_enums.h>

#include "EffectHelper.h"

using namespace android;

using aidl::android::hardware::audio::effect::Descriptor;
using aidl::android::hardware::audio::effect::getEffectTypeUuidHapticGenerator;
using aidl::android::hardware::audio::effect::HapticGenerator;
using aidl::android::hardware::audio::effect::IEffect;
using aidl::android::hardware::audio::effect::IFactory;
using aidl::android::hardware::audio::effect::Parameter;
using android::hardware::audio::common::testing::detail::TestExecutionTracer;

/**
 * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
 * VtsAudioEffectTargetTest.
 */
enum ParamName {
    PARAM_INSTANCE_NAME,
    PARAM_HAPTIC_SCALE_ID,
    PARAM_HAPTIC_SCALE_VIBRATOR_SCALE,
    PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY,
    PARAM_VIBRATION_INFORMATION_Q_FACTOR,
    PARAM_VIBRATION_INFORMATION_MAX_AMPLITUDE,
};
using HapticGeneratorParamTestParam =
        std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int,
                   HapticGenerator::VibratorScale, float, float, float>;

/*
 * Testing parameter range, assuming the parameter supported by effect is in this range.
 * Parameter should be within the valid range defined in the documentation,
 * for any supported value test expects EX_NONE from IEffect.setParameter(),
 * otherwise expect EX_ILLEGAL_ARGUMENT.
 */

// TODO : Update the test values once range/capability is updated by implementation
const int MIN_ID = std::numeric_limits<int>::min();
const int MAX_ID = std::numeric_limits<int>::max();
const float MIN_FLOAT = std::numeric_limits<float>::min();
const float MAX_FLOAT = std::numeric_limits<float>::max();

const std::vector<int> kHapticScaleIdValues = {MIN_ID, 0, MAX_ID};
const std::vector<HapticGenerator::VibratorScale> kVibratorScaleValues = {
        ndk::enum_range<HapticGenerator::VibratorScale>().begin(),
        ndk::enum_range<HapticGenerator::VibratorScale>().end()};

const std::vector<float> kResonantFrequencyValues = {MIN_FLOAT, 100, MAX_FLOAT};
const std::vector<float> kQFactorValues = {MIN_FLOAT, 100, MAX_FLOAT};
const std::vector<float> kMaxAmplitude = {MIN_FLOAT, 100, MAX_FLOAT};

class HapticGeneratorParamTest : public ::testing::TestWithParam<HapticGeneratorParamTestParam>,
                                 public EffectHelper {
  public:
    HapticGeneratorParamTest()
        : mParamHapticScaleId(std::get<PARAM_HAPTIC_SCALE_ID>(GetParam())),
          mParamVibratorScale(std::get<PARAM_HAPTIC_SCALE_VIBRATOR_SCALE>(GetParam())),
          mParamResonantFrequency(
                  std::get<PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY>(GetParam())),
          mParamQFactor(std::get<PARAM_VIBRATION_INFORMATION_Q_FACTOR>(GetParam())),
          mParamMaxAmplitude(std::get<PARAM_VIBRATION_INFORMATION_MAX_AMPLITUDE>(GetParam())) {
        std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
    }
    void SetUp() override {
        ASSERT_NE(nullptr, mFactory);
        ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));

        Parameter::Common common = createParamCommon(
                0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
                kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
        IEffect::OpenEffectReturn ret;
        ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt, &ret, EX_NONE));
        ASSERT_NE(nullptr, mEffect);
    }

    void TearDown() override {
        ASSERT_NO_FATAL_FAILURE(close(mEffect));
        ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
    }

    static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
    std::shared_ptr<IFactory> mFactory;
    std::shared_ptr<IEffect> mEffect;
    Descriptor mDescriptor;
    int mParamHapticScaleId = 0;
    HapticGenerator::VibratorScale mParamVibratorScale = HapticGenerator::VibratorScale::MUTE;
    float mParamResonantFrequency = 0;
    float mParamQFactor = 0;
    float mParamMaxAmplitude = 0;

    void SetAndGetHapticGeneratorParameters() {
        for (auto& it : mTags) {
            auto& tag = std::get<ParamTestEnum::PARAM_TEST_TAG>(it);
            auto& setHg = std::get<ParamTestEnum::PARAM_TEST_TARGET>(it);

            // set parameter
            Parameter expectParam;
            Parameter::Specific specific;
            specific.set<Parameter::Specific::hapticGenerator>(setHg);
            expectParam.set<Parameter::specific>(specific);
            EXPECT_STATUS(EX_NONE, mEffect->setParameter(expectParam)) << expectParam.toString();

            // get parameter
            Parameter getParam;
            Parameter::Id id;
            HapticGenerator::Id hgId;
            hgId.set<HapticGenerator::Id::commonTag>(tag);
            id.set<Parameter::Id::hapticGeneratorTag>(hgId);
            EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
            EXPECT_EQ(expectParam, getParam) << expectParam.toString() << "\n"
                                             << getParam.toString();
        }
    }

    void addHapticScaleParam(int id, HapticGenerator::VibratorScale scale) {
        HapticGenerator setHg;
        std::vector<HapticGenerator::HapticScale> hapticScales = {{.id = id, .scale = scale}};
        setHg.set<HapticGenerator::hapticScales>(hapticScales);
        mTags.push_back({HapticGenerator::hapticScales, setHg});
    }

    void addVibratorInformationParam(float resonantFrequencyHz, float qFactor, float maxAmplitude) {
        HapticGenerator hg;
        HapticGenerator::VibratorInformation vibrationInfo = {
                .resonantFrequencyHz = resonantFrequencyHz,
                .qFactor = qFactor,
                .maxAmplitude = maxAmplitude};
        hg.set<HapticGenerator::vibratorInfo>(vibrationInfo);
        mTags.push_back({HapticGenerator::vibratorInfo, hg});
    }

  private:
    enum ParamTestEnum { PARAM_TEST_TAG, PARAM_TEST_TARGET };
    std::vector<std::tuple<HapticGenerator::Tag, HapticGenerator>> mTags;

    void CleanUp() { mTags.clear(); }
};

TEST_P(HapticGeneratorParamTest, SetAndGetHapticScale) {
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(mParamHapticScaleId, mParamVibratorScale));
    SetAndGetHapticGeneratorParameters();
}

TEST_P(HapticGeneratorParamTest, SetAndGetMultipleHapticScales) {
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(mParamHapticScaleId, mParamVibratorScale));
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(mParamHapticScaleId, mParamVibratorScale));
    SetAndGetHapticGeneratorParameters();
}

TEST_P(HapticGeneratorParamTest, SetAndGetVibratorInformation) {
    EXPECT_NO_FATAL_FAILURE(addVibratorInformationParam(mParamResonantFrequency, mParamQFactor,
                                                        mParamMaxAmplitude));
    SetAndGetHapticGeneratorParameters();
}

INSTANTIATE_TEST_SUITE_P(
        HapticGeneratorValidTest, HapticGeneratorParamTest,
        ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
                                   IFactory::descriptor, getEffectTypeUuidHapticGenerator())),
                           testing::ValuesIn(kHapticScaleIdValues),
                           testing::ValuesIn(kVibratorScaleValues),
                           testing::ValuesIn(kResonantFrequencyValues),
                           testing::ValuesIn(kQFactorValues), testing::ValuesIn(kMaxAmplitude)),
        [](const testing::TestParamInfo<HapticGeneratorParamTest::ParamType>& info) {
            auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
            std::string hapticScaleID = std::to_string(std::get<PARAM_HAPTIC_SCALE_ID>(info.param));
            std::string hapticScaleVibScale = std::to_string(
                    static_cast<int>(std::get<PARAM_HAPTIC_SCALE_VIBRATOR_SCALE>(info.param)));
            std::string resonantFrequency = std::to_string(
                    std::get<PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY>(info.param));
            std::string qFactor =
                    std::to_string(std::get<PARAM_VIBRATION_INFORMATION_Q_FACTOR>(info.param));
            std::string maxAmplitude =
                    std::to_string(std::get<PARAM_VIBRATION_INFORMATION_MAX_AMPLITUDE>(info.param));
            std::string name = getPrefix(descriptor) + "_hapticScaleId" + hapticScaleID +
                               "_hapticScaleVibScale" + hapticScaleVibScale + "_resonantFrequency" +
                               resonantFrequency + "_qFactor" + qFactor + "_maxAmplitude" +
                               maxAmplitude;
            std::replace_if(
                    name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
            return name;
        });

INSTANTIATE_TEST_SUITE_P(
        HapticGeneratorInvalidTest, HapticGeneratorParamTest,
        ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
                                   IFactory::descriptor, getEffectTypeUuidHapticGenerator())),
                           testing::Values(MIN_ID),
                           testing::Values(HapticGenerator::VibratorScale::NONE),
                           testing::Values(MIN_FLOAT), testing::Values(MIN_FLOAT),
                           testing::Values(MIN_FLOAT)),
        [](const testing::TestParamInfo<HapticGeneratorParamTest::ParamType>& info) {
            auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
            std::string hapticScaleID = std::to_string(std::get<PARAM_HAPTIC_SCALE_ID>(info.param));
            std::string hapticScaleVibScale = std::to_string(
                    static_cast<int>(std::get<PARAM_HAPTIC_SCALE_VIBRATOR_SCALE>(info.param)));
            std::string resonantFrequency = std::to_string(
                    std::get<PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY>(info.param));
            std::string qFactor =
                    std::to_string(std::get<PARAM_VIBRATION_INFORMATION_Q_FACTOR>(info.param));
            std::string maxAmplitude =
                    std::to_string(std::get<PARAM_VIBRATION_INFORMATION_MAX_AMPLITUDE>(info.param));
            std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
                               descriptor.common.name + "_UUID_" +
                               toString(descriptor.common.id.uuid) + "_hapticScaleId" +
                               hapticScaleID + "_hapticScaleVibScale" + hapticScaleVibScale +
                               "_resonantFrequency" + resonantFrequency + "_qFactor" + qFactor +
                               "_maxAmplitude" + maxAmplitude;
            std::replace_if(
                    name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
            return name;
        });
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HapticGeneratorParamTest);

// Test HapticScale[] hapticScales parameter
using HapticGeneratorScalesTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>>;
class HapticGeneratorScalesTest : public ::testing::TestWithParam<HapticGeneratorScalesTestParam>,
                                  public EffectHelper {
  public:
    HapticGeneratorScalesTest() {
        std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
    }

    void SetUp() override {
        ASSERT_NE(nullptr, mFactory);
        ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));

        Parameter::Common common = createParamCommon(
                0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
                kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
        IEffect::OpenEffectReturn ret;
        ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt, &ret, EX_NONE));
        ASSERT_NE(nullptr, mEffect);
    }

    void TearDown() override {
        ASSERT_NO_FATAL_FAILURE(close(mEffect));
        ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
        CleanUp();
    }

    static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
    std::shared_ptr<IFactory> mFactory;
    std::shared_ptr<IEffect> mEffect;
    Descriptor mDescriptor;

    void addHapticScaleParam(std::vector<HapticGenerator::HapticScale> scales) {
        mHapticScales.push_back(HapticGenerator::make<HapticGenerator::hapticScales>(scales));
        for (const auto& scale : scales) {
            expectMap.insert_or_assign(scale.id, scale.scale);
        }
    }

    void SetHapticScaleParameters() {
        // std::unordered_set<HapticGenerator::HapticScale> target;
        for (auto& it : mHapticScales) {
            Parameter::Specific specific =
                    Parameter::Specific::make<Parameter::Specific::hapticGenerator>(it);
            Parameter param = Parameter::make<Parameter::specific>(specific);
            EXPECT_STATUS(EX_NONE, mEffect->setParameter(param)) << param.toString();
        }
    }

    void checkHapticScaleParameter() {
        // get parameter
        Parameter targetParam;
        HapticGenerator::Id hgId = HapticGenerator::Id::make<HapticGenerator::Id::commonTag>(
                HapticGenerator::hapticScales);
        Parameter::Id id = Parameter::Id::make<Parameter::Id::hapticGeneratorTag>(hgId);
        EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &targetParam));
        ASSERT_EQ(Parameter::specific, targetParam.getTag());
        Parameter::Specific specific = targetParam.get<Parameter::specific>();
        ASSERT_EQ(Parameter::Specific::hapticGenerator, specific.getTag());
        HapticGenerator hg = specific.get<Parameter::Specific::hapticGenerator>();
        ASSERT_EQ(HapticGenerator::hapticScales, hg.getTag());
        std::vector<HapticGenerator::HapticScale> scales = hg.get<HapticGenerator::hapticScales>();
        ASSERT_EQ(scales.size(), expectMap.size());
        for (const auto& scale : scales) {
            auto itor = expectMap.find(scale.id);
            ASSERT_NE(expectMap.end(), itor);
            ASSERT_EQ(scale.scale, itor->second);
            expectMap.erase(scale.id);
        }
        ASSERT_EQ(0ul, expectMap.size());
    }

    const static HapticGenerator::HapticScale kHapticScaleWithMinId;
    const static HapticGenerator::HapticScale kHapticScaleWithMinIdNew;
    const static HapticGenerator::HapticScale kHapticScale;
    const static HapticGenerator::HapticScale kHapticScaleNew;
    const static HapticGenerator::HapticScale kHapticScaleWithMaxId;
    const static HapticGenerator::HapticScale kHapticScaleWithMaxIdNew;

    std::vector<HapticGenerator> mHapticScales;

    void CleanUp() {
        mHapticScales.clear();
        expectMap.clear();
    }

  private:
    std::map<int /* trackID */, HapticGenerator::VibratorScale> expectMap;
};

const HapticGenerator::HapticScale HapticGeneratorScalesTest::kHapticScaleWithMinId = {
        .id = MIN_ID, .scale = HapticGenerator::VibratorScale::MUTE};
const HapticGenerator::HapticScale HapticGeneratorScalesTest::kHapticScaleWithMinIdNew = {
        .id = MIN_ID, .scale = HapticGenerator::VibratorScale::VERY_LOW};
const HapticGenerator::HapticScale HapticGeneratorScalesTest::kHapticScale = {
        .id = 1, .scale = HapticGenerator::VibratorScale::LOW};
const HapticGenerator::HapticScale HapticGeneratorScalesTest::kHapticScaleNew = {
        .id = 1, .scale = HapticGenerator::VibratorScale::NONE};
const HapticGenerator::HapticScale HapticGeneratorScalesTest::kHapticScaleWithMaxId = {
        .id = MAX_ID, .scale = HapticGenerator::VibratorScale::VERY_HIGH};
const HapticGenerator::HapticScale HapticGeneratorScalesTest::kHapticScaleWithMaxIdNew = {
        .id = MAX_ID, .scale = HapticGenerator::VibratorScale::MUTE};

TEST_P(HapticGeneratorScalesTest, SetAndUpdateOne) {
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam({kHapticScale}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam({kHapticScaleNew}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());

    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam({kHapticScaleWithMinId}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam({kHapticScaleWithMinIdNew}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());

    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam({kHapticScaleWithMaxId}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam({kHapticScaleWithMaxIdNew}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());

    EXPECT_NO_FATAL_FAILURE(checkHapticScaleParameter());
}

TEST_P(HapticGeneratorScalesTest, SetAndUpdateVector) {
    EXPECT_NO_FATAL_FAILURE(
            addHapticScaleParam({kHapticScale, kHapticScaleWithMaxId, kHapticScaleWithMinId}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(
            {kHapticScaleNew, kHapticScaleWithMaxIdNew, kHapticScaleWithMinIdNew}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());

    EXPECT_NO_FATAL_FAILURE(checkHapticScaleParameter());
}

TEST_P(HapticGeneratorScalesTest, SetAndUpdateMultipleVector) {
    EXPECT_NO_FATAL_FAILURE(
            addHapticScaleParam({kHapticScale, kHapticScaleWithMaxId, kHapticScaleWithMinId}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(
            {kHapticScaleNew, kHapticScaleWithMaxIdNew, kHapticScaleWithMinIdNew}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());
    EXPECT_NO_FATAL_FAILURE(
            addHapticScaleParam({kHapticScale, kHapticScaleWithMaxId, kHapticScaleWithMinId}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());

    EXPECT_NO_FATAL_FAILURE(checkHapticScaleParameter());
}

TEST_P(HapticGeneratorScalesTest, SetOneAndAddMoreVector) {
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam({kHapticScale}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam({kHapticScaleWithMaxId, kHapticScaleWithMinId}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());

    EXPECT_NO_FATAL_FAILURE(checkHapticScaleParameter());
}

TEST_P(HapticGeneratorScalesTest, SetMultipleAndAddOneVector) {
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam({kHapticScaleWithMaxId, kHapticScaleWithMinId}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());
    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam({kHapticScale}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());

    EXPECT_NO_FATAL_FAILURE(checkHapticScaleParameter());
}

TEST_P(HapticGeneratorScalesTest, SetMultipleVectorRepeat) {
    EXPECT_NO_FATAL_FAILURE(
            addHapticScaleParam({kHapticScaleWithMaxId, kHapticScale, kHapticScaleWithMinId}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());
    EXPECT_NO_FATAL_FAILURE(
            addHapticScaleParam({kHapticScaleWithMaxId, kHapticScale, kHapticScaleWithMinId}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());
    EXPECT_NO_FATAL_FAILURE(
            addHapticScaleParam({kHapticScaleWithMaxId, kHapticScale, kHapticScaleWithMinId}));
    EXPECT_NO_FATAL_FAILURE(SetHapticScaleParameters());

    EXPECT_NO_FATAL_FAILURE(checkHapticScaleParameter());
}

INSTANTIATE_TEST_SUITE_P(
        HapticGeneratorScalesTest, HapticGeneratorScalesTest,
        ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
                IFactory::descriptor, getEffectTypeUuidHapticGenerator()))),
        [](const testing::TestParamInfo<HapticGeneratorScalesTest::ParamType>& info) {
            auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
            std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
                               descriptor.common.name + "_UUID_" +
                               toString(descriptor.common.id.uuid);
            std::replace_if(
                    name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
            return name;
        });
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HapticGeneratorScalesTest);

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
    ABinderProcess_setThreadPoolMaxThreadCount(1);
    ABinderProcess_startThreadPool();
    return RUN_ALL_TESTS();
}
