/*
 * Copyright (c) 2021-2022 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.
 */

#include "main_ability.h"
#include "hilog_wrapper.h"
#include "test_utils.h"

namespace OHOS {
namespace AppExecFwk {
using namespace OHOS::EventFwk;
namespace {
const int MainAbilityBCode = 300;
constexpr int index_f = 0;
constexpr int index_s = 1;
constexpr int index_t = 2;
constexpr int numThree = 3;
}

void MainAbility::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
    const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
    const sptr<IRemoteObject> &token)
{
    HILOG_INFO("MainAbility::Init");
    Ability::Init(abilityInfo, application, handler, token);
}

void MainAbility::OnStart(const Want &want)
{
    HILOG_INFO("MainAbility::onStart");
    SubscribeEvent();
    Ability::OnStart(want);
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MainAbilityBCode, "onStart");
}

void MainAbility::OnStop()
{
    HILOG_INFO("MainAbility::OnStop");
    Ability::OnStop();
    CommonEventManager::UnSubscribeCommonEvent(subscriber_);
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MainAbilityBCode, "OnStop");
}

void MainAbility::OnActive()
{
    HILOG_INFO("MainAbility::OnActive");
    Ability::OnActive();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MainAbilityBCode, "OnActive");
}

void MainAbility::OnInactive()
{
    HILOG_INFO("MainAbility::OnInactive");
    Ability::OnInactive();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MainAbilityBCode, "OnInactive");
}

void MainAbility::OnBackground()
{
    HILOG_INFO("MainAbility::OnBackground");
    Ability::OnBackground();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MainAbilityBCode, "OnBackground");
}

void MainAbility::OnForeground(const Want &want)
{
    HILOG_INFO("MainAbility::OnForeground");
    Ability::OnForeground(want);
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, MainAbilityBCode, "OnForeground");
}

void MainAbility::OnAbilityResult(int requestCode, int resultCode, const Want &resultData)
{
    HILOG_INFO("MainAbility::OnAbilityResult");
    Ability::OnAbilityResult(requestCode, resultCode, resultData);
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, 0, resultData.ToUri());
}

void MainAbility::OnBackPressed()
{
    HILOG_INFO("MainAbility::OnBackPressed");
    Ability::OnBackPressed();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, 0, "");
}

void MainAbility::OnNewWant(const Want &want)
{
    HILOG_INFO("MainAbility::OnNewWant");
    Ability::OnNewWant(want);
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB_LIFECYCLE, 0, want.ToUri());
}

MainAbility::~MainAbility()
{
    CommonEventManager::UnSubscribeCommonEvent(subscriber_);
}

void MainAbility::SubscribeEvent()
{
    std::vector<std::string> eventList = {
        g_EVENT_REQU_FIRSTB,
    };
    MatchingSkills matchingSkills;
    for (const auto &e : eventList) {
        matchingSkills.AddEvent(e);
    }
    CommonEventSubscribeInfo subscribeInfo(matchingSkills);
    subscribeInfo.SetPriority(1);
    subscriber_ = std::make_shared<FirstEventSubscriber>(subscribeInfo);
    subscriber_->mainAbility = this;
    CommonEventManager::SubscribeCommonEvent(subscriber_);
}

void FirstEventSubscriber::OnReceiveEvent(const CommonEventData &data)
{
    HILOG_INFO("FirstEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
    HILOG_INFO("FirstEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
    HILOG_INFO("FirstEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
    auto eventName = data.GetWant().GetAction();
    if (std::strcmp(eventName.c_str(), g_EVENT_REQU_FIRSTB.c_str()) == 0) {
        auto target = data.GetData();
        auto caseInfo = TestUtils::split(target, "_");
        if (caseInfo.size() < numThree) {
            return;
        }
        if (mapTestFunc_.find(caseInfo[index_f]) != mapTestFunc_.end()) {
            mapTestFunc_[caseInfo[index_f]](std::stoi(caseInfo[index_s]), std::stoi(caseInfo[index_t]), data.GetCode());
        } else {
            HILOG_INFO("OnReceiveEvent: CommonEventData error(%{public}s)", target.c_str());
        }
    }
}

void MainAbility::TestAbility(int apiIndex, int caseIndex, int code)
{
    HILOG_INFO("MainAbility::TestAbility");
    if (mapCase_.find(apiIndex) != mapCase_.end()) {
        if (caseIndex < (int)mapCase_[apiIndex].size()) {
            mapCase_[apiIndex][caseIndex](code);
        }
    }
}

void MainAbility::GetApplicationInfoCase1(int code)
{
    return;
}
void MainAbility::GetApplicationInfoCase2(int code)
{
    return;
}
void MainAbility::GetApplicationInfoCase3(int code)
{
    auto appInfo = AbilityContext::GetApplicationInfo();
    string result = "";
    if (appInfo != nullptr) {
        result = appInfo->name;
    }

    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, result);
}

void MainAbility::GetCacheDirCase1(int code)
{
    return;
}
void MainAbility::GetCacheDirCase2(int code)
{
    return;
}
void MainAbility::GetCacheDirCase3(int code)
{
    string cacheDir = AbilityContext::GetCacheDir();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, cacheDir);
}

void MainAbility::GetDatabaseDirCase1(int code)
{
    return;
}
void MainAbility::GetDatabaseDirCase2(int code)
{
    return;
}
void MainAbility::GetDatabaseDirCase3(int code)
{
    string databaseDir = AbilityContext::GetDatabaseDir();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, databaseDir);
}

void MainAbility::GetDataDirCase1(int code)
{
    return;
}

void MainAbility::GetDataDirCase2(int code)
{
    return;
}

void MainAbility::GetDataDirCase3(int code)
{
    string dataDir = AbilityContext::GetDataDir();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, dataDir);
}

void MainAbility::GetDirCase1(int code)
{
    return;
}

void MainAbility::GetDirCase2(int code)
{
    return;
}

void MainAbility::GetDirCase3(int code)
{
    string name = "getDir";
    string dir = AbilityContext::GetDir(name, 0);
    dir = AbilityContext::GetDir(name, 0);
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, dir);
}

void MainAbility::GetNoBackupFilesDirCase1(int code)
{
    return;
}

void MainAbility::GetBundleManagerCase1(int code)
{
    return;
}

void MainAbility::VerifyCallingPermissionCase1(int code)
{
    return;
}

void MainAbility::VerifyPermissionCase1(int code)
{
    return;
}

void MainAbility::VerifySelfPermissionCase1(int code)
{
    return;
}

void MainAbility::GetBundleCodePathCase1(int code)
{
    return;
}

void MainAbility::GetBundleCodePathCase2(int code)
{
    return;
}

void MainAbility::GetBundleCodePathCase3(int code)
{
    string bundleCodePath = AbilityContext::GetBundleCodePath();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, bundleCodePath);
}

void MainAbility::GetBundleNameCase1(int code)
{
    return;
}

void MainAbility::GetBundleNameCase2(int code)
{
    return;
}

void MainAbility::GetBundleNameCase3(int code)
{
    string bundleName = AbilityContext::GetBundleName();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, bundleName);
    return;
}

void MainAbility::GetBundleResourcePathCase1(int code)
{
    return;
}

void MainAbility::GetBundleResourcePathCase2(int code)
{
    return;
}

void MainAbility::GetBundleResourcePathCase3(int code)
{
    string bundleResourcePath = AbilityContext::GetBundleResourcePath();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, bundleResourcePath);
}

void MainAbility::CanRequestPermissionCase1(int code)
{
    return;
}

void MainAbility::GetCallingAbilityCase1(int code)
{
    return;
}

void MainAbility::GetCallingAbilityCase2(int code)
{
    auto callingAbility = AbilityContext::GetCallingAbility();
    string result = "";
    if (callingAbility != nullptr)
        result = callingAbility->GetURI();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, result);
    return;
}

void MainAbility::GetCallingAbilityCase3(int code)
{
    string targetBundle = "com.ohos.amsst.AppKitA";
    string targetAbility = "SecondAbility";
    Want want;
    want.SetElementName(targetBundle, targetAbility);
    want.SetParam("operator", 0);
    StartAbility(want);
}

void MainAbility::GetContextCase1(int code)
{
    return;
}

void MainAbility::GetAbilityManagerCase1(int code)
{
    return;
}

void MainAbility::GetProcessInfoCase1(int code)
{
    return;
}

void MainAbility::GetProcessInfoCase2(int code)
{
    return;
}

void MainAbility::GetProcessInfoCase3(int code)
{
    auto processInfo = AbilityContext::GetProcessInfo();
    string result = "";
    if (processInfo != nullptr) {
        result = processInfo->GetProcessName();
    }

    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, result);
}

void MainAbility::GetAppTypeCase1(int code)
{
    return;
}

void MainAbility::GetAppTypeCase2(int code)
{
    return;
}

void MainAbility::GetAppTypeCase3(int code)
{
    string appType = AbilityContext::GetAppType();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, appType);
}

void MainAbility::GetCallingBundleCase1(int code)
{
    return;
}

void MainAbility::GetCallingBundleCase2(int code)
{
    string callingBundle = AbilityContext::GetCallingBundle();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, callingBundle);
}

void MainAbility::GetCallingBundleCase3(int code)
{
    string targetBundle = "com.ohos.amsst.AppKitA";
    string targetAbility = "SecondAbility";
    Want want;
    want.SetElementName(targetBundle, targetAbility);
    want.SetParam("operator", 0);
    StartAbility(want);
}

void MainAbility::StartAbilityCase1(int code)
{
    return;
}

void MainAbility::StartAbilityCase2(int code)
{
    return;
}

void MainAbility::TerminateAbilityCase1(int code)
{
    return;
}

void MainAbility::GetElementNameCase1(int code)
{
    return;
}

void MainAbility::GetElementNameCase2(int code)
{
    return;
}

void MainAbility::GetElementNameCase3(int code)
{
    auto elementName = AbilityContext::GetElementName();
    string result = "";
    if (elementName != nullptr)
        result = elementName->GetBundleName();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, result);
}

void MainAbility::GetHapModuleInfoCase1(int code)
{
    return;
}

void MainAbility::GetHapModuleInfoCase2(int code)
{
    return;
}

void MainAbility::GetHapModuleInfoCase3(int code)
{
    auto elementName = AbilityContext::GetHapModuleInfo();
    string result = "";
    if (elementName != nullptr)
        result = elementName->moduleName;
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, result);
}

void MainAbility::GetCodeCacheDirCase1(int code)
{}
void MainAbility::GetCodeCacheDirCase2(int code)
{}
void MainAbility::GetCodeCacheDirCase3(int code)
{
    string codeCacheDir = AbilityContext::GetCodeCacheDir();
    TestUtils::PublishEvent(g_EVENT_RESP_FIRSTB, code, codeCacheDir);
}

void MainAbility::GetApplicationContextCase1(int code)
{}

REGISTER_AA(MainAbility)
}  // namespace AppExecFwk
}  // namespace OHOS
