<commit_before>//
//  FeedSocialWorkCell.swift
//  Yep
//
//  Created by nixzhu on 15/11/19.
//  Copyright © 2015年 Catch Inc. All rights reserved.
//

import UIKit
import Kingfisher
import Ruler
import RealmSwift
import MapKit

private let dribbbleShotHeight: CGFloat = Ruler.iPhoneHorizontal(160, 200, 220).value
private let linkContainerViewHeight: CGFloat = Ruler.iPhoneHorizontal(44, 50, 50).value

class FeedSocialWorkCell: FeedBasicCell {

    @IBOutlet weak var logoImageView: UIImageView!

    @IBOutlet weak var socialWorkContainerView: UIView!

    @IBOutlet weak var mediaContainerView: UIView!
    @IBOutlet weak var socialWorkImageView: UIImageView!

    @IBOutlet weak var linkContainerView: UIView!
    @IBOutlet weak var linkImageView: UIImageView!
    @IBOutlet weak var linkLabel: UILabel!
    @IBOutlet weak var linkAccessoryImageView: UIImageView!
    @IBOutlet weak var linkContainerViewHeightConstraint: NSLayoutConstraint!

    @IBOutlet weak var githubRepoContainerView: UIView!
    @IBOutlet weak var githubRepoImageView: UIImageView!
    @IBOutlet weak var githubRepoNameLabel: UILabel!
    @IBOutlet weak var githubRepoDescriptionLabel: UILabel!
    @IBOutlet weak var githubRepoAccessoryImageView: UIImageView!

    @IBOutlet weak var voiceContainerView: UIView!
    @IBOutlet weak var voiceBubbleImageVIew: UIImageView!
    @IBOutlet weak var voicePlayButton: UIButton!
    @IBOutlet weak var voiceSampleView: SampleView!
    @IBOutlet weak var voiceTimeLabel: UILabel!

    @IBOutlet weak var voiceSampleViewWidthConstraint: NSLayoutConstraint!

    @IBOutlet weak var locationContainerView: UIView!
    @IBOutlet weak var locationMapImageView: UIImageView!
    @IBOutlet weak var locationNameLabel: UILabel!

    @IBOutlet weak var socialWorkBorderImageView: UIImageView!
    @IBOutlet weak var socialWorkContainerViewHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var githubRepoImageViewTrailingConstraint: NSLayoutConstraint!

    lazy var socialWorkMaskImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "social_media_image_mask"))
        return imageView
    }()

    var feed: DiscoveredFeed?

    var tapGithubRepoLinkAction: (NSURL -> Void)?
    var tapDribbbleShotLinkAction: (NSURL -> Void)?
    var tapDribbbleShotMediaAction: ((transitionView: UIView, image: UIImage?, imageURL: NSURL, linkURL: NSURL) -> Void)?
    var tapLocationAction: ((locationName: String, locationCoordinate: CLLocationCoordinate2D) -> Void)?

    var audioPlaying: Bool = false {
        willSet {
            if newValue != audioPlaying {
                if newValue {
                    voicePlayButton.setImage(UIImage(named: "icon_pause"), forState: .Normal)
                } else {
                    voicePlayButton.setImage(UIImage(named: "icon_play"), forState: .Normal)
                }
            }
        }
    }
    var playOrPauseAudioAction: (FeedSocialWorkCell -> Void)?
    var audioPlayedDuration: NSTimeInterval = 0 {
        willSet {
            updateVoiceContainerView()
        }
    }
    private func updateVoiceContainerView() {

        guard let feed = feed, realm = try? Realm(), feedAudio = FeedAudio.feedAudioWithFeedID(feed.id, inRealm: realm) else {
            return
        }

        if let (audioDuration, audioSamples) = feedAudio.audioMetaInfo {

            voiceSampleView.samples = audioSamples
            voiceSampleView.progress = CGFloat(audioPlayedDuration / audioDuration)
        }

        if let playingFeedAudio = YepAudioService.sharedManager.playingFeedAudio where playingFeedAudio.feedID == feedAudio.feedID, let audioPlayer = YepAudioService.sharedManager.audioPlayer where audioPlayer.playing {
            audioPlaying = true
        } else {
            audioPlaying = false
        }
    }

    static let messageTextViewMaxWidth: CGFloat = {
        let maxWidth = UIScreen.mainScreen().bounds.width - (15 + 40 + 10 + 15)
        return maxWidth
    }()

    override func layoutSubviews() {
        super.layoutSubviews()

        if feed?.hasSocialImage ?? false {
            socialWorkMaskImageView.frame = socialWorkImageView.bounds
        }

        if feed?.hasMapImage ?? false {
            socialWorkMaskImageView.frame = locationMapImageView.bounds
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        githubRepoAccessoryImageView.tintColor = UIColor.yepCellAccessoryImageViewTintColor()
        linkAccessoryImageView.tintColor = UIColor.yepCellAccessoryImageViewTintColor()
        voiceBubbleImageVIew.tintColor = UIColor.leftBubbleTintColor()
        voicePlayButton.tintColor = UIColor.lightGrayColor()
        voicePlayButton.tintAdjustmentMode = .Normal
        voiceTimeLabel.textColor = UIColor.lightGrayColor()

        let tapDribbbleMedia = UITapGestureRecognizer(target: self, action: "tapDribbbleMedia:")
        mediaContainerView.addGestureRecognizer(tapDribbbleMedia)

        let tapDribbbleLink = UITapGestureRecognizer(target: self, action: "tapDribbbleLink:")<commit_msg>prepareForReuse<commit_after>//
//  FeedSocialWorkCell.swift
//  Yep
//
//  Created by nixzhu on 15/11/19.
//  Copyright © 2015年 Catch Inc. All rights reserved.
//

import UIKit
import Kingfisher
import Ruler
import RealmSwift
import MapKit

private let dribbbleShotHeight: CGFloat = Ruler.iPhoneHorizontal(160, 200, 220).value
private let linkContainerViewHeight: CGFloat = Ruler.iPhoneHorizontal(44, 50, 50).value

class FeedSocialWorkCell: FeedBasicCell {

    @IBOutlet weak var logoImageView: UIImageView!

    @IBOutlet weak var socialWorkContainerView: UIView!

    @IBOutlet weak var mediaContainerView: UIView!
    @IBOutlet weak var socialWorkImageView: UIImageView!

    @IBOutlet weak var linkContainerView: UIView!
    @IBOutlet weak var linkImageView: UIImageView!
    @IBOutlet weak var linkLabel: UILabel!
    @IBOutlet weak var linkAccessoryImageView: UIImageView!
    @IBOutlet weak var linkContainerViewHeightConstraint: NSLayoutConstraint!

    @IBOutlet weak var githubRepoContainerView: UIView!
    @IBOutlet weak var githubRepoImageView: UIImageView!
    @IBOutlet weak var githubRepoNameLabel: UILabel!
    @IBOutlet weak var githubRepoDescriptionLabel: UILabel!
    @IBOutlet weak var githubRepoAccessoryImageView: UIImageView!

    @IBOutlet weak var voiceContainerView: UIView!
    @IBOutlet weak var voiceBubbleImageVIew: UIImageView!
    @IBOutlet weak var voicePlayButton: UIButton!
    @IBOutlet weak var voiceSampleView: SampleView!
    @IBOutlet weak var voiceTimeLabel: UILabel!

    @IBOutlet weak var voiceSampleViewWidthConstraint: NSLayoutConstraint!

    @IBOutlet weak var locationContainerView: UIView!
    @IBOutlet weak var locationMapImageView: UIImageView!
    @IBOutlet weak var locationNameLabel: UILabel!

    @IBOutlet weak var socialWorkBorderImageView: UIImageView!
    @IBOutlet weak var socialWorkContainerViewHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var githubRepoImageViewTrailingConstraint: NSLayoutConstraint!

    lazy var socialWorkMaskImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "social_media_image_mask"))
        return imageView
    }()

    var feed: DiscoveredFeed?

    var tapGithubRepoLinkAction: (NSURL -> Void)?
    var tapDribbbleShotLinkAction: (NSURL -> Void)?
    var tapDribbbleShotMediaAction: ((transitionView: UIView, image: UIImage?, imageURL: NSURL, linkURL: NSURL) -> Void)?
    var tapLocationAction: ((locationName: String, locationCoordinate: CLLocationCoordinate2D) -> Void)?

    var audioPlaying: Bool = false {
        willSet {
            if newValue != audioPlaying {
                if newValue {
                    voicePlayButton.setImage(UIImage(named: "icon_pause"), forState: .Normal)
                } else {
                    voicePlayButton.setImage(UIImage(named: "icon_play"), forState: .Normal)
                }
            }
        }
    }
    var playOrPauseAudioAction: (FeedSocialWorkCell -> Void)?
    var audioPlayedDuration: NSTimeInterval = 0 {
        willSet {
            updateVoiceContainerView()
        }
    }
    private func updateVoiceContainerView() {

        guard let feed = feed, realm = try? Realm(), feedAudio = FeedAudio.feedAudioWithFeedID(feed.id, inRealm: realm) else {
            return
        }

        if let (audioDuration, audioSamples) = feedAudio.audioMetaInfo {

            voiceSampleView.samples = audioSamples
            voiceSampleView.progress = CGFloat(audioPlayedDuration / audioDuration)
        }

        if let playingFeedAudio = YepAudioService.sharedManager.playingFeedAudio where playingFeedAudio.feedID == feedAudio.feedID, let audioPlayer = YepAudioService.sharedManager.audioPlayer where audioPlayer.playing {
            audioPlaying = true
        } else {
            audioPlaying = false
        }
    }

    static let messageTextViewMaxWidth: CGFloat = {
        let maxWidth = UIScreen.mainScreen().bounds.width - (15 + 40 + 10 + 15)
        return maxWidth
    }()

    override func layoutSubviews() {
        super.layoutSubviews()

        if feed?.hasSocialImage ?? false {
            socialWorkMaskImageView.frame = socialWorkImageView.bounds
        }

        if feed?.hasMapImage ?? false {
            socialWorkMaskImageView.frame = locationMapImageView.bounds
        }
    }

    override func prepareForReuse() {
        super.prepareForReuse()

        locationMapImageView.image = nil
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        githubRepoAccessoryImageView.tintColor = UIColor.yepCellAccessoryImageViewTintColor()
        linkAccessoryImageView.tintColor = UIColor.yepCellAccessoryImageViewTintColor()
        voiceBubbleImageVIew.tintColor = UIColor.leftBubbleTintColor()
        voicePlayButton.tintColor = UIColor.lightGrayColor()
        voicePlayButton.tintAdjustmentMode = .Normal
        voiceTimeLabel.textColor = UIColor.lightGrayColor()

        let tapDribbbleMedia = UITapGestureRecognizer(target: self, action: "tapDribbbleMedia:")
        mediaContainerView.addGestureRecognizer(tapDribbbleMedia)

        let tapDribbbleLink = UITapGestureRecognizer(target: self, action: "tapDribbbleLink:")
{'fraction_non_alphanumeric': 0.07414985871577512, 'fraction_numerical': 0.006820617753093637, 'mean_word_length': 3.3713798977853493, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '252220', 'n_tokens_mistral': 3110, 'n_tokens_neox': 2880, 'n_words': 687}