//
//  TSMirrorCameraView.swift
//  Girly
//
//  Created by 100Years on 2025/1/8.
//

class TSMirrorCameraView: TSBaseView {
    
    private var shwoWidth:CGFloat = k_ScreenWidth - 32
    var imageNamed:String = "mirror_round" {
        didSet{
            if UIImage(named: imageNamed) != nil {
                setShapeView(shapeView: cameraView)
            }
        }
    }

    lazy var noPermissionView: TSMirrorCameraNoPermissionView = {
        let noPermissionView = TSMirrorCameraNoPermissionView()
        noPermissionView.isHidden = true
        return noPermissionView
    }()
    
    
    lazy var cameraManager: CameraManager = {
        let cameraManager = CameraManager()
        cameraManager.cameraDevice = CameraDevice.front
        cameraManager.cameraOutputQuality = .high
        cameraManager.showAccessPermissionPopupAutomatically = true
        return cameraManager
    }()
    
    
    lazy var cameraView: UIView = {
        let cameraView = UIView()
        cameraView.frame = CGRect(x: 0, y: 0, width: shwoWidth, height: shwoWidth)
        return cameraView
    }()
    
    lazy var notDeterminedView: UIView = {
        let notDeterminedView = UIView()

        return notDeterminedView
    }()
    
    
    override func creatUI() {
        
        contentView.addSubview(cameraView)
        cameraView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }
        
        contentView.addSubview(noPermissionView)
        noPermissionView.snp.makeConstraints { make in
            make.top.equalTo(111)
            make.leading.trailing.equalTo(0)
        }
        
        self.layoutIfNeeded()
        cameraManager.askUserForCameraPermission { [weak self]  permissionGranted in
            guard let self = self else { return }
            
            if permissionGranted {
                noPermissionView.isHidden = true
                self.addCameraToView()
                kDelayMainShort {
                    self.setShapeView(shapeView: self.cameraView)
                }
            } else {
                noPermissionView.isHidden = false
            }
        }
    }
    
    func setShapeView(shapeView:UIView){
        if let image = UIImage(named: imageNamed) {
            shapeView.setImageMask(image: image)
        }
    }
    
    
    fileprivate func addCameraToView() {
        cameraManager.addPreviewLayerToView(cameraView)
//        cameraManager.showErrorBlock = { [weak self] (erTitle: String, erMessage: String) -> Void in
//            debugPrint(erMessage)
//        }
    }
    
    
    func startCapture() {
        cameraManager.resumeCaptureSession()
    }
    
    func endCapture() {
        cameraManager.stopCaptureSession()
    }
}



class TSMirrorCameraNoPermissionView: TSBaseView {
    
    lazy var imageView: UIImageView = {
        let imageView = UIImageView.createImageView(imageName: "nopermission")
        return imageView
    }()
    
    lazy var textLabel: UILabel = {
        let textLabel = UILabel.createLabel(text: "No camera permission. Can't look in the mirror.",font: .font(size: 14.0),textColor: .assist,numberOfLines: 0)
        textLabel.setLineSpacing(4,alignment: .center)
        return textLabel
    }()
    
    lazy var submitBtn: UIButton = {
        let btn = createNormalSubmitBtn(title: "Open Now") {
            UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
        }
        btn.cornerRadius = 22
        return btn
    }()
    
    override func creatUI() {
        contentView.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(0)
            make.width.equalTo(120)
            make.height.equalTo(120)
        }
        
        contentView.addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(imageView.snp.bottom).offset(14.0)
            make.left.equalTo(74)
            make.right.equalTo(-74)
        }
        
        contentView.addSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(textLabel.snp.bottom).offset(38.0)
            make.left.equalTo(50)
            make.right.equalTo(-50)
            make.height.equalTo(44.0)
            make.bottom.equalToSuperview()
        }
    }
}
