/*
 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 PQEditVisionTrackModel: PQEditBaseModel {
    @objc dynamic public var count: Int = 0
    @objc dynamic public var duration: Float64 = 0
    public var visionTrackMaterials = List<PQEditVisionTrackMaterialsModel>.init()

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

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

    // 取到指定类型素材 "image" "video" "gif"
   public  func getEnableVisionTrackMaterials(type:String) -> List<PQEditVisionTrackMaterialsModel> {
        let visionTrackMaterialsTemp = List<PQEditVisionTrackMaterialsModel>.init()


        for visionTrackMaterialsModel in visionTrackMaterials {
            if visionTrackMaterialsModel.type == type   {
                visionTrackMaterialsTemp.append(visionTrackMaterialsModel)
            }
        }
        return visionTrackMaterialsTemp
    }
    
    // 取到有效的素材 image video gif
   public  func getEnableVisionTrackMaterials() -> List<PQEditVisionTrackMaterialsModel> {
        let visionTrackMaterialsTemp = List<PQEditVisionTrackMaterialsModel>.init()

//        BFLog(message: "visionTrackMaterials 总数\(visionTrackMaterials.count)")
        for visionTrackMaterialsModel in visionTrackMaterials {
            if visionTrackMaterialsModel.type == "image" || visionTrackMaterialsModel.type == "video" || visionTrackMaterialsModel.type == "gif" {
                visionTrackMaterialsTemp.append(visionTrackMaterialsModel)
            }
        }
//        BFLog(message: "visionTrackMaterials 有效素材总数\(visionTrackMaterialsTemp.count)")
        return visionTrackMaterialsTemp
    }

//    （video,image, gif)
//    （video,image, gif,subtitle)
    /// 交换有效视觉位置 不包括字幕
    /// - Parameters:
    ///   - at: 原位置
    ///   - to: 目标位
   public  func updateVisionTrackMaterialsIndex(at: Int, to: Int) {
        // 如果有字幕缓存字幕，交换位置后在恢复原数组
        let subtitlesTemp = List<PQEditVisionTrackMaterialsModel>.init()
        for subtitle in getSubtitleMatraislInfo() {
            subtitlesTemp.append(subtitle)
        }

        visionTrackMaterials = getEnableVisionTrackMaterials()

        // 交换位置
        let atItem = getEnableVisionTrackMaterials()[at]
        visionTrackMaterials.remove(at: at)
        visionTrackMaterials.insert(atItem, at: to)

        // 恢复字幕
        for subtitle in subtitlesTemp {
            visionTrackMaterials.append(subtitle)
        }
    }

    /// 取录音有效字幕数据
    /// - Returns:
   public  func getSubtitleMatraislInfo() -> List<PQEditVisionTrackMaterialsModel> {
        let visionTrackMaterialsTemp = List<PQEditVisionTrackMaterialsModel>.init()

        BFLog(message: "visionTrackMaterials 总数\(visionTrackMaterials.count)")
        for visionTrackMaterialsModel in visionTrackMaterials {
            if visionTrackMaterialsModel.type == "subtitle" {
                visionTrackMaterialsTemp.append(visionTrackMaterialsModel)
            }
        }
        BFLog(message: "visionTrackMaterials 本段有效字幕总数\(visionTrackMaterialsTemp.count)")
        return visionTrackMaterialsTemp
    }

    /// 删除某个视觉素材，不包括字幕
    /// - Parameter materialsModel: GIF . IMAGE, video 类型的
   public  func deleteMatraislInfo(materialsModel: PQEditVisionTrackMaterialsModel?) {
        if materialsModel == nil {
            BFLog(message: "要删除的素材为空 操作不成功！")
            return
        }
        let index = visionTrackMaterials.firstIndex(where: { (item) -> Bool in
            item.isEqualMaterial(newMaterial: materialsModel)
        })
        if index != nil, visionTrackMaterials.count > (index ?? 0) {
            visionTrackMaterials.remove(at: index!)
        }
    }
}
