/*
 Copyright (c) 2020 Swift Models Generated from JSON powered by http://www.json4swift.com

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 For support, please feel free to contact me at https://www.linkedin.com/in/syedabsar

 */

import Foundation

import ObjectMapper
import RealmSwift
import BFCommonKit

public class PQEditAudioTrackModel: PQEditBaseModel {
    public var audioTrackMaterials: List<PQEditAudioTrackMaterialModel> = List<PQEditAudioTrackMaterialModel>.init()
    @objc dynamic public var count: Int = 0
    @objc dynamic public var duration: Float64 = 0

    required convenience public init?(map _: Map) {
        self.init()
    }

    required public  init() {
        super.init()
    }

    override public func mapping(map: Map) {
        audioTrackMaterials <- (map["audioTrackMaterials"], PQListTransform<PQEditAudioTrackMaterialModel>())
        count <- map["count"]
        duration <- (map["duration"], timeTransform)
    }

    /// 根据 VoiceType 取音轨信息
    /// - Parameter voiceType: voiceType  音频类型（bgm：背景音乐，produce：合成语音，speech：录音,local : 本地文件）
    /// - Returns: PQEditAudioTrackMaterialModel
   public  func getAudioTrackModel(voiceType: String) -> PQEditAudioTrackMaterialModel? {
        BFLog(message: "查询 voiceType is \(voiceType)")
        var audioTrackMaterialModel: PQEditAudioTrackMaterialModel?
        for materialModel in audioTrackMaterials {
            if materialModel.voiceType == voiceType {
                audioTrackMaterialModel = materialModel
            }
        }
        return audioTrackMaterialModel
    }

    // 添加录音文件 如果原来有先删除旧数据
   public  func addSpeechAudioTrackMaterialModel(_ audioTrackMaterialModel: PQEditAudioTrackMaterialModel) {
        // 1 找出声音转文字的对应段落
        let sesscionIdex = audioTrackMaterials.firstIndex(where: { (audioTrack) -> Bool in
            audioTrack.voiceType == VOICETYPT.SPEECH.rawValue
        })
        if audioTrackMaterials.count > sesscionIdex ?? 0 {
            BFLog(message: "原来有录音 \(String(describing: sesscionIdex))")
            audioTrackMaterials.remove(at: sesscionIdex ?? 0)
        }
        audioTrackMaterials.append(audioTrackMaterialModel)
    }
}
