/*
 * Copyright (c) 2024 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 "ability_manager_radar.h"

#include "ability_manager_errors.h"
#include "hisysevent_report.h"
#include "hilog_tag_wrapper.h"

namespace OHOS {
namespace AAFWK {
using namespace OHOS::AAFwk;
namespace {
constexpr const char* ORG_PKG_NAME = "ohos.abilitymanagerservice";
constexpr const char* APPLICATION_CONTINUE_BEHAVIOR = "APPLICATION_CONTINUE_BEHAVIOR";
constexpr const char* DMS_PKG_NAME = "ohos.distributedschedule";
constexpr const char* ORG_PKG = "ORG_PKG";
constexpr const char* FUNC = "FUNC";
constexpr const char* BIZ_SCENE = "BIZ_SCENE";
constexpr const char* BIZ_STAGE = "BIZ_STAGE";
constexpr const char* STAGE_RES = "STAGE_RES";
constexpr const char* TO_CALL_PKG = "TO_CALL_PKG";
constexpr const char* ERROR_CODE = "ERROR_CODE";
constexpr char APP_CONTINUE_DOMAIN[] = "APP_CONTINUE";
static constexpr unsigned int FLAG_ABILITY_CONTINUATION = 0x00000008;
}
ContinueRadar &ContinueRadar::GetInstance()
{
    static ContinueRadar instance;
    return instance;
}

bool ContinueRadar::ClickIconContinue(const std::string& func)
{
    auto hisyseventReport = std::make_shared<AAFwk::HisyseventReport>(5);
    hisyseventReport->InsertParam(ORG_PKG, ORG_PKG_NAME);
    hisyseventReport->InsertParam(FUNC, func);
    hisyseventReport->InsertParam(BIZ_SCENE, static_cast<int32_t>(BizScene::CLICK_ICON));
    hisyseventReport->InsertParam(BIZ_STAGE, static_cast<int32_t>(ClickIcon::CLICKICON_CONTINUE));
    hisyseventReport->InsertParam(STAGE_RES, static_cast<int32_t>(StageRes::STAGE_SUCC));
    int res = hisyseventReport->Report(APP_CONTINUE_DOMAIN,
        APPLICATION_CONTINUE_BEHAVIOR, HISYSEVENT_BEHAVIOR);
    if (res != ERR_OK) {
        TAG_LOGE(AAFwkTag::DEFAULT, "error, res:%{public}d", res);
        return false;
    }
    return true;
}

bool ContinueRadar::ClickIconStartAbility(const std::string& func, unsigned int flags, int32_t errCode)
{
    if ((flags & FLAG_ABILITY_CONTINUATION) == 0) {
        TAG_LOGD(AAFwkTag::DEFAULT, "StartAbility not support continuation!");
        return false;
    }
    int32_t res = ERR_OK;
    StageRes stageRes = (errCode == ERR_OK) ? StageRes::STAGE_SUCC : StageRes::STAGE_FAIL;
    if (stageRes == StageRes::STAGE_SUCC) {
        auto hisyseventReport = std::make_shared<AAFwk::HisyseventReport>(5);
        hisyseventReport->InsertParam(ORG_PKG, ORG_PKG_NAME);
        hisyseventReport->InsertParam(FUNC, func);
        hisyseventReport->InsertParam(BIZ_SCENE, static_cast<int32_t>(BizScene::CLICK_ICON));
        hisyseventReport->InsertParam(BIZ_STAGE, static_cast<int32_t>(ClickIcon::CLICKICON_STARTABILITY));
        hisyseventReport->InsertParam(STAGE_RES, static_cast<int32_t>(StageRes::STAGE_SUCC));
        res = hisyseventReport->Report(APP_CONTINUE_DOMAIN, APPLICATION_CONTINUE_BEHAVIOR, HISYSEVENT_BEHAVIOR);
    } else {
        auto hisyseventReport = std::make_shared<AAFwk::HisyseventReport>(6);
        hisyseventReport->InsertParam(ORG_PKG, ORG_PKG_NAME);
        hisyseventReport->InsertParam(FUNC, func);
        hisyseventReport->InsertParam(BIZ_SCENE, static_cast<int32_t>(BizScene::CLICK_ICON));
        hisyseventReport->InsertParam(BIZ_STAGE, static_cast<int32_t>(ClickIcon::CLICKICON_STARTABILITY));
        hisyseventReport->InsertParam(STAGE_RES, static_cast<int32_t>(StageRes::STAGE_FAIL));
        hisyseventReport->InsertParam(ERROR_CODE, errCode);
        res = hisyseventReport->Report(APP_CONTINUE_DOMAIN, APPLICATION_CONTINUE_BEHAVIOR, HISYSEVENT_BEHAVIOR);
    }
    if (res != ERR_OK) {
        TAG_LOGE(AAFwkTag::DEFAULT, "error, res:%{public}d", res);
        return false;
    }
    return true;
}

bool ContinueRadar::ClickIconRecvOver(const std::string& func)
{
    int32_t res = ERR_OK;
    auto hisyseventReport = std::make_shared<AAFwk::HisyseventReport>(5);
    hisyseventReport->InsertParam(ORG_PKG, ORG_PKG_NAME);
    hisyseventReport->InsertParam(FUNC, func);
    hisyseventReport->InsertParam(BIZ_SCENE, static_cast<int32_t>(BizScene::CLICK_ICON));
    hisyseventReport->InsertParam(BIZ_STAGE, static_cast<int32_t>(ClickIcon::CLICKICON_RECV_OVER));
    hisyseventReport->InsertParam(STAGE_RES, static_cast<int32_t>(StageRes::STAGE_SUCC));
    res = hisyseventReport->Report(APP_CONTINUE_DOMAIN, APPLICATION_CONTINUE_BEHAVIOR, HISYSEVENT_BEHAVIOR);
    if (res != ERR_OK) {
        TAG_LOGE(AAFwkTag::DEFAULT, "error, res:%{public}d", res);
        return false;
    }
    return true;
}

bool ContinueRadar::SaveDataContinue(const std::string& func)
{
    auto hisyseventReport = std::make_shared<AAFwk::HisyseventReport>(5);
    hisyseventReport->InsertParam(ORG_PKG, ORG_PKG_NAME);
    hisyseventReport->InsertParam(FUNC, func);
    hisyseventReport->InsertParam(BIZ_SCENE, static_cast<int32_t>(BizScene::SAVE_DATA));
    hisyseventReport->InsertParam(BIZ_STAGE, static_cast<int32_t>(SaveData::SAVEDATA_CONTINUE));
    hisyseventReport->InsertParam(STAGE_RES, static_cast<int32_t>(StageRes::STAGE_SUCC));
    int32_t res = hisyseventReport->Report(APP_CONTINUE_DOMAIN, APPLICATION_CONTINUE_BEHAVIOR, HISYSEVENT_BEHAVIOR);
    if (res != ERR_OK) {
        TAG_LOGE(AAFwkTag::DEFAULT, "error, res:%{public}d", res);
        return false;
    }
    return true;
}

bool ContinueRadar::SaveDataRes(const std::string& func)
{
    auto hisyseventReport = std::make_shared<AAFwk::HisyseventReport>(5);
    hisyseventReport->InsertParam(ORG_PKG, ORG_PKG_NAME);
    hisyseventReport->InsertParam(FUNC, func);
    hisyseventReport->InsertParam(BIZ_SCENE, static_cast<int32_t>(BizScene::SAVE_DATA));
    hisyseventReport->InsertParam(BIZ_STAGE, static_cast<int32_t>(SaveData::SAVEDATA_RES));
    hisyseventReport->InsertParam(STAGE_RES, static_cast<int32_t>(StageRes::STAGE_SUCC));
    int32_t res = hisyseventReport->Report(APP_CONTINUE_DOMAIN, APPLICATION_CONTINUE_BEHAVIOR, HISYSEVENT_BEHAVIOR);
    if (res != ERR_OK) {
        TAG_LOGE(AAFwkTag::DEFAULT, "error, res:%{public}d", res);
        return false;
    }
    return true;
}

bool ContinueRadar::SaveDataRemoteWant(const std::string& func)
{
    auto hisyseventReport = std::make_shared<AAFwk::HisyseventReport>(6);
    hisyseventReport->InsertParam(ORG_PKG, ORG_PKG_NAME);
    hisyseventReport->InsertParam(FUNC, func);
    hisyseventReport->InsertParam(BIZ_SCENE, static_cast<int32_t>(BizScene::SAVE_DATA));
    hisyseventReport->InsertParam(BIZ_STAGE, static_cast<int32_t>(SaveData::SAVEDATA_REMOTE_WANT));
    hisyseventReport->InsertParam(STAGE_RES, static_cast<int32_t>(StageRes::STAGE_SUCC));
    hisyseventReport->InsertParam(TO_CALL_PKG, DMS_PKG_NAME);
    int32_t res = hisyseventReport->Report(APP_CONTINUE_DOMAIN, APPLICATION_CONTINUE_BEHAVIOR, HISYSEVENT_BEHAVIOR);
    if (res != ERR_OK) {
        TAG_LOGE(AAFwkTag::DEFAULT, "error, res:%{public}d", res);
        return false;
    }
    return true;
}

} // namespace AAFWK
} // namespace OHOS