//
//  CCSpatialPlayController.swift
//  SwiftProject
//
//  Created by aaa on 2024/1/29.
//

//空间视频+空间转码
import UIKit
import AVKit
import AVFoundation


class CCSpatialPlayController: BaseController {

    var mTopImgView:UIImageView?
    var mAvPlayer:AVPlayerViewController?
    
    var mCenView:UIView?
    var mCenLab1:UILabel?
    var mCenLab2:UILabel?
    var mCenBtn:UIButton?
    
    var mLabel:UILabel?
    var mTabView:CCSpatialPlayView?
    
    var mBottomBtn:UIButton?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.setNavgationBarColor(color: UIColor.clear)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.black
        self.navLine?.isHidden = true
        self.setNavgaionTitle(string: "视频转码")
        
        //393*236
        mTopImgView = UIImageView(frame: CGRect(x: 0, y: 0, width: SCREEN_Width, height: SCREEN_Height * 236/393))
        view.addSubview(mTopImgView!)
        mTopImgView?.image = UIImage.init(named: "BG_Top")
        self.view.bringSubviewToFront(self.navtionBar!)
        
        
        //345*200
        mAvPlayer = AVPlayerViewController()
        mAvPlayer?.view.frame = CGRect(x: 24, y: Int(SafeAreaTop_Height) + 24, width: Int(SCREEN_Width) - 48, height: (Int(SCREEN_Width) - 48) * 200/345)
        self.addChild(mAvPlayer!)
        self.view.addSubview(mAvPlayer!.view)
        mAvPlayer?.view.backgroundColor = UIColor.hexStringToColor(hexString: "#666666")
        mAvPlayer!.view.clipsToBounds = true
        mAvPlayer!.view.layer.cornerRadius = 8
        
        let videoURL = Bundle.main.url(forResource: "ddd_video", withExtension: "mp4")
        let player = AVPlayer(url: videoURL!)
        mAvPlayer!.player = player
        player.play()
        
        
        mBottomBtn = UIButton.init(type: UIButton.ButtonType.custom)
        self.view.addSubview(mBottomBtn!)
        mBottomBtn!.backgroundColor = UIColor.hexStringToColor(hexString: "#5326D6")
        mBottomBtn?.tag = 10
        mBottomBtn?.isSelected = false
        mBottomBtn?.addTarget(self, action: #selector(navgationButtonClick2(sender:)), for: UIControl.Event.touchUpInside)
        mBottomBtn?.frame = CGRect(x: 24, y: 10, width: SCREEN_Width - 48, height: 54)
        mBottomBtn!.bottom = SCREEN_Height - SafeAreaBottom_Height - 30
        mBottomBtn!.clipsToBounds = true
        mBottomBtn!.layer.cornerRadius = 8
        mBottomBtn!.setTitle("转码导出", for: UIControl.State.normal)
        mBottomBtn!.setTitleColor(UIColor.white, for: UIControl.State.normal)
        mBottomBtn!.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        
        mCenView = UIView()
        self.view.addSubview(mCenView!)
        mCenView?.frame = CGRect(x: 24, y: Int(mAvPlayer!.view.bottom) + 16, width: Int(SCREEN_Width) - 48, height: 70)
        mCenView?.clipsToBounds = true
        mCenView?.layer.cornerRadius = 12
        mCenView?.backgroundColor = UIColor.colorWithRGB(_r: 18, _g: 7, _b: 68)
        
        mCenLab1 = UILabel()
        mCenLab1!.bounds = CGRect(x: 0, y: 0, width: 100, height: 0)
        mCenLab1!.textColor = UIColor.hexStringToColor(hexString: "#ffffff")
        mCenLab1!.font = UIFont.systemFont(ofSize: 14)
        mCenView!.addSubview(mCenLab1!)
        mCenLab1!.text = "20240121020131292.MOV"
        mCenLab1!.sizeToFit()
        if(mCenLab1!.width > mCenView!.width - 140){
            mCenLab1!.width = mCenView!.width - 140
        }
        mCenLab1!.top = 12
        mCenLab1!.left = 16
        
        
        mCenLab2 = UILabel()
        mCenLab2!.bounds = CGRect(x: 0, y: 0, width: 100, height: 0)
        mCenLab2!.textColor = UIColor.hexStringToColor(hexString: "#67617A")
        mCenLab2!.font = UIFont.systemFont(ofSize: 12)
        mCenView!.addSubview(mCenLab2!)
        mCenLab2!.text = "00:00:03 · 10.9 MB"
        mCenLab2!.sizeToFit()
        if(mCenLab2!.width > mCenView!.width - 140){
            mCenLab2!.width = mCenView!.width - 140
        }
        mCenLab2!.top = 38
        mCenLab2!.left = 16
        
        mCenBtn = UIButton.init(type: UIButton.ButtonType.custom)
        mCenView!.addSubview(mCenBtn!)
        mCenBtn!.backgroundColor = UIColor.hexStringToColor(hexString: "#5326D6")
        mCenBtn?.tag = 11
        mCenBtn?.isSelected = false
        mCenBtn?.addTarget(self, action: #selector(navgationButtonClick2(sender:)), for: UIControl.Event.touchUpInside)
        mCenBtn?.frame = CGRect(x: 24, y: 10, width: 84, height: 36)
        mCenBtn!.centerY = mCenView!.height * 0.5
        mCenBtn!.right = mCenView!.width - 16
        mCenBtn!.clipsToBounds = true
        mCenBtn!.layer.cornerRadius = 10
        mCenBtn!.setTitle("MV-HEVC", for: UIControl.State.normal)
        mCenBtn!.setTitleColor(UIColor.white, for: UIControl.State.normal)
        mCenBtn!.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        
        
        mLabel = UILabel()
        mLabel!.bounds = CGRect(x: 0, y: 0, width: 100, height: 0)
        mLabel!.textColor = UIColor.hexStringToColor(hexString: "#6A696A")
        mLabel!.font = UIFont.systemFont(ofSize: 14)
        self.view.addSubview(mLabel!)
        mLabel!.text = "格式"
        mLabel!.sizeToFit()
        mLabel!.top = mCenView!.bottom + 32
        mLabel!.left = 24
        
        let maxHi = mBottomBtn!.top - mCenView!.bottom - 110
        var hi = 62.0*5
        if(hi > maxHi){hi = maxHi}
        mTabView = CCSpatialPlayView.init(frame: CGRect(x: 24, y: mCenView!.bottom + 70, width: SCREEN_Width - 48, height: hi))
        self.view.addSubview(mTabView!)
        mTabView!.backgroundColor = UIColor.hexStringToColor(hexString: "#1F1E20")
        mTabView?.clipsToBounds = true
        mTabView?.layer.cornerRadius = 12
        mTabView?.handle = {dic in
            print(dic)
        }
        
    }
    
    @objc public func navgationButtonClick2(sender:UIButton){
        
        print("nihao")
        
        //侧边栏
        if(sender.tag == 11){
            
        }
    }

}

