<commit_before>//
//  BMPlayerControlView.swift
//  Pods
//
//  Created by BrikerMan on 16/4/29.
//
//

import UIKit
import NVActivityIndicatorView


class BMPlayerControlView: UIView, BMPlayerCustomControlView {
    
    weak var delegate: BMPlayerControlViewDelegate?
    var playerTitleLabel        : UILabel?  { get { return  titleLabel } }
    var playerCurrentTimeLabel  : UILabel?  { get { return  currentTimeLabel } }
    var playerTotalTimeLabel    : UILabel?  { get { return  totalTimeLabel } }
    
    var playerPlayButton        : UIButton? { get { return  playButton } }
    var playerFullScreenButton  : UIButton? { get { return  fullScreenButton } }
    var playerBackButton        : UIButton? { get { return  backButton } }
    var playerReplayButton      : UIButton? { get { return  centerButton } }
    var playerRatioButton       : UIButton? { get { return  ratioButton }}
    
    var playerTimeSlider        : UISlider? { get { return  timeSlider } }
    var playerProgressView      : UIProgressView? { get { return  progressView } }
    
    var playerSlowButton        : UIButton? { get { return  slowButton } }
    var playerMirrorButton      : UIButton? { get { return  mirrorButton } }
    
    var getView: UIView { return self }
    
    /// 主体
    var mainMaskView    = UIView()
    var topMaskView     = UIView()
    var bottomMaskView  = UIView()
    var maskImageView   = UIImageView()
    
    /// 顶部
    var backButton  = UIButton(type: UIButtonType.custom)
    var titleLabel  = UILabel()
    var chooseDefitionView = UIView()
    var ratioButton = UIButton(type: .custom)       //调整视频画面比例按钮 Added by toodoo
    
    /// 底部
    var currentTimeLabel = UILabel()
    var totalTimeLabel   = UILabel()
    
    var timeSlider       = BMTimeSlider()
    var progressView     = UIProgressView()
    
    var playButton       = UIButton(type: UIButtonType.custom)
    var fullScreenButton = UIButton(type: UIButtonType.custom)
    var slowButton       = UIButton(type: UIButtonType.custom)
    var mirrorButton     = UIButton(type: UIButtonType.custom)
    
    
    
    /// 中间部分
    var loadingIndector  = NVActivityIndicatorView(frame:  CGRect(x: 0, y: 0, width: 30, height: 30))
    
    var seekToView       = UIView()
    var seekToViewImage  = UIImageView()
    var seekToLabel      = UILabel()
    
    var centerButton     = UIButton(type: UIButtonType.custom)
    
    var videoItems:[BMPlayerItemDefinitionProtocol] = []
    
    var selectedIndex = 0
    
    fileprivate var isSelectecDefitionViewOpened = false
    
    var isFullScreen = false
    
    // MARK: - funcitons
    func showPlayerUIComponents() {
        topMaskView.alpha    = 1.0
        bottomMaskView.alpha = 1.0
        mainMaskView.backgroundColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.4 )
        
        if isFullScreen {
            chooseDefitionView.alpha = 1.0
        }
    }
    
    func hidePlayerUIComponents() {
        centerButton.isHidden = true
        topMaskView.alpha    = 0.0
        bottomMaskView.alpha = 0.0
        mainMaskView.backgroundColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 )
        
        chooseDefitionView.snp.updateConstraints { (make) in
            make.height.equalTo(35)
        }
        chooseDefitionView.alpha = 0.0
    }
    
    func aspectRatioChanged(_ state:BMPlayerAspectRatio) {
        switch state {
        case .default:
            ratioButton.setBackgroundImage(BMImageResourcePath("BMPlayer_ratio"), for: UIControlState())
            break
        case .sixteen2NINE:
            ratioButton.setBackgroundImage(BMImageResourcePath("BMPlayer_169"), for: UIControlState())
            break
        case .four2THREE:
            ratioButton.setBackgroundImage(BMImageResourcePath("BMPlayer_43"), for: UIControlState())
            break
        }
    }
    
    func updateUI(_ isForFullScreen: Bool) {
        isFullScreen = isForFullScreen
        if isForFullScreen {
            if BMPlayerConf.slowAndMirror {
                self.slowButton.isHidden = false
                self.mirrorButton.isHidden = false
                
                fullScreenButton.snp.remakeConstraints { (make) in
                    make.width.equalTo(50)
                    make.height.equalTo(50)
                    make.centerY.equalTo(currentTimeLabel)
                    make.left.equalTo(slowButton.snp.right)
                    make.right.equalTo(bottomMaskView.snp.right)
                }
            }
            fullScreenButton.setImage(BMImageResourcePath("BMPlayer_portialscreen"), for: UIControlState())
            ratioButton.isHidden = false
            chooseDefitionView.isHidden = false
            if BMPlayerConf.topBarShowInCase.rawValue == 2 {
                topMaskView.isHidden = true
            } else {
                topMaskView.isHidden = false
            }
        } else {
            if BMPlayerConf.topBarShowInCase.rawValue >= 1 {
                topMaskView.isHidden = true
            } else {
                topMaskView.isHidden = false
            }
            ratioButton.isHidden = true
            chooseDefitionView.isHidden = true
            
            self.slowButton.isHidden = true
            self.mirrorButton.isHidden = true
            fullScreenButton.setImage(BMImageResourcePath("BMPlayer_fullscreen"), for: UIControlState())
            fullScreenButton.snp.remakeConstraints { (make) in
                make.width.equalTo(50)
                make.height.equalTo(50)
                make.centerY.equalTo(currentTimeLabel)
                make.left.equalTo(totalTimeLabel.snp.right)
                make.right.equalTo(bottomMaskView.snp.right)
            }
        }
        
        if !BMPlayerConf.showScaleChangeButton {
            ratioButton.isHidden = true
        }
    }
    
    func showPlayToTheEndView() {
        centerButton.isHidden = false
    }
    
    func showLoader() {
        loadingIndector.isHidden = false
        loadingIndector.startAnimating()
    }
    
    func hideLoader() {
        loadingIndector.isHidden = true
    }
    
    func showSeekToView(_ toSecound: TimeInterval, isAdd: Bool) {
        seekToView.isHidden   = false
        let Min = Int(toSecound / 60)
        let Sec = Int(toSecound.truncatingRemainder(dividingBy: 60))
        seekToLabel.text    = String(format: "%02d:%02d", Min, Sec)
        let rotate = isAdd ? 0 : CGFloat(M_PI)
        seekToViewImage.transform = CGAffineTransform(rotationAngle: rotate)
    }
    
    func hideSeekToView() {
        seekToView.isHidden = true
    }
    
    func showCoverWithLink(_ cover:String) {
        if let url = URL(string: cover) {
            DispatchQueue.global(qos: .default).async {
                let data = try? Data(contentsOf: url) //make sure your image in this url does exist, otherwise unwrap in a if let check
                DispatchQueue.main.async(execute: {
                    self.maskImageView.image = UIImage(data: data!)
                    self.hideLoader()
                });
            }<commit_msg>修复 cover url 下载失败导致 carsh<commit_after>//
//  BMPlayerControlView.swift
//  Pods
//
//  Created by BrikerMan on 16/4/29.
//
//

import UIKit
import NVActivityIndicatorView


class BMPlayerControlView: UIView, BMPlayerCustomControlView {
    
    weak var delegate: BMPlayerControlViewDelegate?
    var playerTitleLabel        : UILabel?  { get { return  titleLabel } }
    var playerCurrentTimeLabel  : UILabel?  { get { return  currentTimeLabel } }
    var playerTotalTimeLabel    : UILabel?  { get { return  totalTimeLabel } }
    
    var playerPlayButton        : UIButton? { get { return  playButton } }
    var playerFullScreenButton  : UIButton? { get { return  fullScreenButton } }
    var playerBackButton        : UIButton? { get { return  backButton } }
    var playerReplayButton      : UIButton? { get { return  centerButton } }
    var playerRatioButton       : UIButton? { get { return  ratioButton }}
    
    var playerTimeSlider        : UISlider? { get { return  timeSlider } }
    var playerProgressView      : UIProgressView? { get { return  progressView } }
    
    var playerSlowButton        : UIButton? { get { return  slowButton } }
    var playerMirrorButton      : UIButton? { get { return  mirrorButton } }
    
    var getView: UIView { return self }
    
    /// 主体
    var mainMaskView    = UIView()
    var topMaskView     = UIView()
    var bottomMaskView  = UIView()
    var maskImageView   = UIImageView()
    
    /// 顶部
    var backButton  = UIButton(type: UIButtonType.custom)
    var titleLabel  = UILabel()
    var chooseDefitionView = UIView()
    var ratioButton = UIButton(type: .custom)       //调整视频画面比例按钮 Added by toodoo
    
    /// 底部
    var currentTimeLabel = UILabel()
    var totalTimeLabel   = UILabel()
    
    var timeSlider       = BMTimeSlider()
    var progressView     = UIProgressView()
    
    var playButton       = UIButton(type: UIButtonType.custom)
    var fullScreenButton = UIButton(type: UIButtonType.custom)
    var slowButton       = UIButton(type: UIButtonType.custom)
    var mirrorButton     = UIButton(type: UIButtonType.custom)
    
    
    
    /// 中间部分
    var loadingIndector  = NVActivityIndicatorView(frame:  CGRect(x: 0, y: 0, width: 30, height: 30))
    
    var seekToView       = UIView()
    var seekToViewImage  = UIImageView()
    var seekToLabel      = UILabel()
    
    var centerButton     = UIButton(type: UIButtonType.custom)
    
    var videoItems:[BMPlayerItemDefinitionProtocol] = []
    
    var selectedIndex = 0
    
    fileprivate var isSelectecDefitionViewOpened = false
    
    var isFullScreen = false
    
    // MARK: - funcitons
    func showPlayerUIComponents() {
        topMaskView.alpha    = 1.0
        bottomMaskView.alpha = 1.0
        mainMaskView.backgroundColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.4 )
        
        if isFullScreen {
            chooseDefitionView.alpha = 1.0
        }
    }
    
    func hidePlayerUIComponents() {
        centerButton.isHidden = true
        topMaskView.alpha    = 0.0
        bottomMaskView.alpha = 0.0
        mainMaskView.backgroundColor = UIColor ( red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 )
        
        chooseDefitionView.snp.updateConstraints { (make) in
            make.height.equalTo(35)
        }
        chooseDefitionView.alpha = 0.0
    }
    
    func aspectRatioChanged(_ state:BMPlayerAspectRatio) {
        switch state {
        case .default:
            ratioButton.setBackgroundImage(BMImageResourcePath("BMPlayer_ratio"), for: UIControlState())
            break
        case .sixteen2NINE:
            ratioButton.setBackgroundImage(BMImageResourcePath("BMPlayer_169"), for: UIControlState())
            break
        case .four2THREE:
            ratioButton.setBackgroundImage(BMImageResourcePath("BMPlayer_43"), for: UIControlState())
            break
        }
    }
    
    func updateUI(_ isForFullScreen: Bool) {
        isFullScreen = isForFullScreen
        if isForFullScreen {
            if BMPlayerConf.slowAndMirror {
                self.slowButton.isHidden = false
                self.mirrorButton.isHidden = false
                
                fullScreenButton.snp.remakeConstraints { (make) in
                    make.width.equalTo(50)
                    make.height.equalTo(50)
                    make.centerY.equalTo(currentTimeLabel)
                    make.left.equalTo(slowButton.snp.right)
                    make.right.equalTo(bottomMaskView.snp.right)
                }
            }
            fullScreenButton.setImage(BMImageResourcePath("BMPlayer_portialscreen"), for: UIControlState())
            ratioButton.isHidden = false
            chooseDefitionView.isHidden = false
            if BMPlayerConf.topBarShowInCase.rawValue == 2 {
                topMaskView.isHidden = true
            } else {
                topMaskView.isHidden = false
            }
        } else {
            if BMPlayerConf.topBarShowInCase.rawValue >= 1 {
                topMaskView.isHidden = true
            } else {
                topMaskView.isHidden = false
            }
            ratioButton.isHidden = true
            chooseDefitionView.isHidden = true
            
            self.slowButton.isHidden = true
            self.mirrorButton.isHidden = true
            fullScreenButton.setImage(BMImageResourcePath("BMPlayer_fullscreen"), for: UIControlState())
            fullScreenButton.snp.remakeConstraints { (make) in
                make.width.equalTo(50)
                make.height.equalTo(50)
                make.centerY.equalTo(currentTimeLabel)
                make.left.equalTo(totalTimeLabel.snp.right)
                make.right.equalTo(bottomMaskView.snp.right)
            }
        }
        
        if !BMPlayerConf.showScaleChangeButton {
            ratioButton.isHidden = true
        }
    }
    
    func showPlayToTheEndView() {
        centerButton.isHidden = false
    }
    
    func showLoader() {
        loadingIndector.isHidden = false
        loadingIndector.startAnimating()
    }
    
    func hideLoader() {
        loadingIndector.isHidden = true
    }
    
    func showSeekToView(_ toSecound: TimeInterval, isAdd: Bool) {
        seekToView.isHidden   = false
        let Min = Int(toSecound / 60)
        let Sec = Int(toSecound.truncatingRemainder(dividingBy: 60))
        seekToLabel.text    = String(format: "%02d:%02d", Min, Sec)
        let rotate = isAdd ? 0 : CGFloat(M_PI)
        seekToViewImage.transform = CGAffineTransform(rotationAngle: rotate)
    }
    
    func hideSeekToView() {
        seekToView.isHidden = true
    }
    
    func showCoverWithLink(_ cover:String) {
        if let url = URL(string: cover) {
            DispatchQueue.global(qos: .default).async {
                let data = try? Data(contentsOf: url) //make sure your image in this url does exist, otherwise unwrap in a if let check
                DispatchQueue.main.async(execute: {
                    if let data = data {
                        self.maskImageView.image = UIImage(data: data)
                    } else {
                        self.maskImageView.image = nil
                    }
                    self.hideLoader()
                });
            }
{'fraction_non_alphanumeric': 0.08242753623188406, 'fraction_numerical': 0.00947603121516165, 'mean_word_length': 1.9291836734693877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5096098', 'n_tokens_mistral': 4274, 'n_tokens_neox': 4128, 'n_words': 841}