//
//  BluetoothDebuggerView.swift
//  MVM5G
//
//  Created by Matt Bruce on 3/19/22.
//  Copyright © 2022 Kyle. All rights reserved.
//

import Foundation
import UIKit

/// View Used to show on Pairing screens to act as a Console Log
class BluetoothDebuggerView: UIView {
    
    //console log view
    var textView = UITextView()
    
    //text show in textView
    var text: String = "Initial Page Load" {
        didSet{
            textView.text = text
        }
    }
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }
    
    init() {
        super.init(frame: .zero)
        setupView()
    }
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    private func setupView() {
        translatesAutoresizingMaskIntoConstraints = false

        let doneButton = UIButton()
        doneButton.setTitle("Done", for: .normal)
        doneButton.addTarget(self, action: #selector(close), for: .touchUpInside)
        doneButton.translatesAutoresizingMaskIntoConstraints = false
        
        let headerView = UIView()
        headerView.translatesAutoresizingMaskIntoConstraints = false
        headerView.backgroundColor = .mvmCoolGray6
        headerView.addSubview(doneButton)
        doneButton.rightAnchor.constraint(equalTo: headerView.rightAnchor).isActive = true
        doneButton.topAnchor.constraint(equalTo: headerView.topAnchor).isActive = true
        doneButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
        doneButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
        
        textView.setContentCompressionResistancePriority(.required, for: .vertical)
        textView.isUserInteractionEnabled = true
        textView.showsVerticalScrollIndicator = true
        textView.isEditable = false
        textView.isScrollEnabled = true
        textView.scrollRangeToVisible(NSMakeRange(0, 0))
        textView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
        
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.backgroundColor = .white
        textView.text = text
        addSubview(headerView)
        addSubview(textView)
        
        headerView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        headerView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
        headerView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        headerView.bottomAnchor.constraint(equalTo: textView.topAnchor).isActive = true
        headerView.heightAnchor.constraint(equalToConstant: 40).isActive = true

        textView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        textView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
        textView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        textView.heightAnchor.constraint(equalToConstant: 400).isActive = true
    }

    //removes self from View
    @objc func close(sender: UIButton){
        removeFromSuperview()
    }

}
