/*
 * Copyright 2021 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.
 */

#pragma once

#include <aidl/android/hardware/tv/tuner/BnFrontend.h>
#include <fstream>
#include <iostream>
#include <thread>
#include "Tuner.h"
#include "dtv_plugin.h"

using namespace std;

namespace aidl {
namespace android {
namespace hardware {
namespace tv {
namespace tuner {

class Tuner;

const int TUNE_BUFFER_SIZE = 1;        // byte
const int TUNE_BUFFER_TIMEOUT = 2000;  // ms

class Frontend : public BnFrontend {
  public:
    Frontend(FrontendType type, int32_t id);

    ::ndk::ScopedAStatus setCallback(
            const std::shared_ptr<IFrontendCallback>& in_callback) override;
    ::ndk::ScopedAStatus tune(const FrontendSettings& in_settings) override;
    ::ndk::ScopedAStatus stopTune() override;
    ::ndk::ScopedAStatus close() override;
    ::ndk::ScopedAStatus scan(const FrontendSettings& in_settings,
                              FrontendScanType in_type) override;
    ::ndk::ScopedAStatus stopScan() override;
    ::ndk::ScopedAStatus getStatus(const std::vector<FrontendStatusType>& in_statusTypes,
                                   std::vector<FrontendStatus>* _aidl_return) override;
    ::ndk::ScopedAStatus setLnb(int32_t in_lnbId) override;
    ::ndk::ScopedAStatus linkCiCam(int32_t in_ciCamId, int32_t* _aidl_return) override;
    ::ndk::ScopedAStatus unlinkCiCam(int32_t in_ciCamId) override;
    ::ndk::ScopedAStatus getHardwareInfo(std::string* _aidl_return) override;
    ::ndk::ScopedAStatus removeOutputPid(int32_t in_pid) override;
    ::ndk::ScopedAStatus getFrontendStatusReadiness(
            const std::vector<FrontendStatusType>& in_statusTypes,
            std::vector<FrontendStatusReadiness>* _aidl_return) override;

    binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;

    FrontendType getFrontendType();
    int32_t getFrontendId();
    string getSourceFile();
    dtv_plugin* getIptvPluginInterface();
    string getIptvTransportDescription();
    dtv_streamer* getIptvPluginStreamer();
    void readTuneByte(void* buf);
    void* getTuneByteBuffer() { return mTuneByteBuffer; };
    dtv_streamer* createIptvPluginStreamer(dtv_plugin* interface, const char* transport_desc);
    dtv_plugin* createIptvPluginInterface();
    bool isLocked();
    void getFrontendInfo(FrontendInfo* _aidl_return);
    void setTunerService(std::shared_ptr<Tuner> tuner);

  private:
    virtual ~Frontend();
    bool supportsSatellite();
    void scanThreadLoop();

    std::shared_ptr<IFrontendCallback> mCallback;
    std::shared_ptr<Tuner> mTuner;
    FrontendType mType = FrontendType::UNDEFINED;
    int32_t mId = 0;
    bool mIsLocked = false;
    int32_t mCiCamId;
    std::thread mScanThread;
    FrontendSettings mFrontendSettings;
    FrontendScanType mFrontendScanType;
    std::ifstream mFrontendData;
    FrontendCapabilities mFrontendCaps;
    vector<FrontendStatusType> mFrontendStatusCaps;
    dtv_plugin* mIptvPluginInterface;
    string mIptvTransportDescription;
    dtv_streamer* mIptvPluginStreamer;
    std::thread mIptvFrontendTuneThread;
    void* mTuneByteBuffer = nullptr;
};

}  // namespace tuner
}  // namespace tv
}  // namespace hardware
}  // namespace android
}  // namespace aidl