VerificationView.swift 11.2 KB
//
//  VerificationView.swift
//  InterQR-Internship
//
//  Created by Дмитрий Тимофеев on 30.05.2022.
//

import Foundation
import UIKit

class VerificationView: UIView {
    
    var isShow: Bool = false
    
    var logoImage: UIImageView = {
        var view = UIImageView()
        view.image = UIImage(named: "InterQR")
        return view
    }()
    var greetingsLabel: UILabel = {
        var label = UILabel()
        label.text = "Hello"
        label.font = UIFont(name: SkModernistFontType.bold.rawValue, size: 35)
        return label
    }()
    var verifyLabel: UILabel = {
        var label = UILabel()
        label.text = "Lets verify your \naccount"
        label.numberOfLines = 2
        label.font = UIFont(name: SkModernistFontType.regular.rawValue, size: 28)
        return label
    }()
    var shieldImage: UIImageView = {
        var view = UIImageView()
        view.image = UIImage(named: "Shield")
        return view
    }()
    var chooseLabel: UILabel = {
        var label = UILabel()
        label.text = "Choose what to start with"
        label.font = UIFont(name: SkModernistFontType.bold.rawValue, size: 16)
        label.font = .systemFont(ofSize: 16, weight: .bold)
        return label
    }()
    var emailButton: Checkbox = {
        var button = Checkbox()
        button.checkboxTitle = "Email"
        button.isSelected = true
        return button
    }()

    var phoneNumberButton: Checkbox = {
        var button = Checkbox()
        button.checkboxTitle = "Phone Number"
        return button
    }()

    var dividingLineView: UIView = {
        var view = UIView()
        view.backgroundColor = UIColor(red: 246/255, green: 246/255, blue: 246/255, alpha: 1)
        return view
    }()
    
    var enterChosenAuthenticatorLabel: UILabel = {
        var label = UILabel()
        label.text = "Enter your email address"
        label.font = UIFont(name: SkModernistFontType.bold.rawValue, size: 16)
        return label
    }()
    
    var emailTextField: UITextField = {
        var textField = UITextField()
        textField.backgroundColor = UIColor(red: 243/255, green: 243/255, blue: 243/255, alpha: 1)
        textField.textAlignment = .left
        textField.layer.cornerRadius = 15
        textField.keyboardType = .emailAddress
        textField.placeholder = "Example: john.doe@gmail.com"
        textField.font = UIFont(name: SkModernistFontType.regular.rawValue, size: 15)
        textField.layer.masksToBounds = true
        textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 22, height: textField.frame.height))
        textField.leftViewMode = .always
        textField.returnKeyType = .done
        return textField
    }()
    
    var phoneTextField: TextFieldWithPadding = {
        var textField = TextFieldWithPadding()
        textField.backgroundColor = UIColor(red: 243/255, green: 243/255, blue: 243/255, alpha: 1)
        textField.textAlignment = .left
        textField.layer.cornerRadius = 15
        textField.keyboardType = .numberPad
        textField.text = "+1"
        textField.font = UIFont(name: SkModernistFontType.regular.rawValue, size: 15)
        textField.layer.masksToBounds = true
        textField.isHidden = true
        return textField
    }()
    
    var verificationDescriptionLabel: UILabel = {
        var label = UILabel()
        label.numberOfLines = 0
        label.text = "Verification code will be sent to your e-mail"
        label.font = UIFont(name: "Sk-Modernist", size: 14)
        label.font = .systemFont(ofSize: 14, weight: .regular)
        return label
    }()
    
    var continueButton: UIButton = {
        var button = UIButton()
        button.setImage(UIImage(named: "RectangleWithShadow"), for: .normal)
        button.clipsToBounds = true
        button.contentMode = .scaleAspectFit
        button.layer.cornerRadius = 15
        return button
    }()
    
    var continueLabel: UILabel = {
        var label = UILabel()
        label.text = "Continue"
        label.font = UIFont(name: SkModernistFontType.bold.rawValue, size: 16)
        label.textColor = .white
        return label
    }()
    
    var pointerImage: UIImageView = {
        var image = UIImageView()
        image.image = UIImage(named: "Pointer")
        return image
    }()
    
    var phonePickerView: PhoneNumberView = {
        var view = PhoneNumberView()
        view.isUserInteractionEnabled = true
        return view
    }()
    
    var showPickerButton: UIButton = {
        var button = UIButton()
        button.isHidden = true
        return button
    }()
    
    var flagPickerView: UIImageView = {
        var image = UIImageView()
        image.image = UIImage(named: "USA")
        image.layer.cornerRadius = 11
        image.contentMode = .scaleAspectFit
        return image
    }()
    
    var flagPointerView: UIImageView = {
        var image = UIImageView()
        image.image = UIImage(named: "FlagPointer")
        return image
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        layout()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func layout() {
        
        addSubview(logoImage)
        addSubview(greetingsLabel)
        addSubview(verifyLabel)
        addSubview(shieldImage)
        
        addSubview(chooseLabel)
        addSubview(emailButton)
        addSubview(phoneNumberButton)
        addSubview(dividingLineView)
        
        addSubview(enterChosenAuthenticatorLabel)
        addSubview(emailTextField)
        addSubview(phoneTextField)
        addSubview(verificationDescriptionLabel)
        phoneTextField.addSubview(showPickerButton)
        
        showPickerButton.addSubview(flagPickerView)
        showPickerButton.addSubview(flagPointerView)
        
        addSubview(continueButton)
        continueButton.addSubview(continueLabel)
        continueButton.addSubview(pointerImage)
        
        addSubview(phonePickerView)
        
        logoImage.snp.makeConstraints {
            $0.top.equalTo(snp.top).offset(80)
            $0.left.equalTo(snp.left).offset(27)
        }
        greetingsLabel.snp.makeConstraints {
            $0.top.equalTo(logoImage.snp.bottom).offset(42)
            $0.left.equalTo(snp.left).offset(24)
        }
        shieldImage.snp.makeConstraints {
            $0.right.equalToSuperview()
            $0.top.equalTo(logoImage.snp.bottom).offset(27)
        }
        verifyLabel.snp.makeConstraints {
            $0.left.equalTo(27)
            $0.top.equalTo(greetingsLabel.snp.bottom).offset(10)
        }
        chooseLabel.snp.makeConstraints {
            $0.top.equalTo(shieldImage.snp.bottom).offset(52)
            $0.left.equalTo(snp.left).offset(27)
        }
        emailButton.snp.makeConstraints {
            $0.left.equalTo(snp.left).offset(27)
            $0.top.equalTo(chooseLabel.snp.bottom).offset(29)
            $0.height.equalTo(20)
        }
        phoneNumberButton.snp.makeConstraints {
            $0.top.equalTo(emailButton.snp.top)
            $0.height.equalTo(20)
            $0.left.equalTo(emailButton.snp.right).offset(44)
        }
        dividingLineView.snp.makeConstraints {
            $0.left.right.equalToSuperview()
            $0.top.equalTo(emailButton.snp.bottom).offset(41)
            $0.height.equalTo(1)
        }
        enterChosenAuthenticatorLabel.snp.makeConstraints {
            $0.top.equalTo(dividingLineView.snp.bottom).offset(39)
            $0.left.equalTo(logoImage.snp.left)
        }
        emailTextField.snp.makeConstraints {
            $0.left.equalTo(logoImage.snp.left)
            $0.right.equalTo(snp.right).offset(-27)
            $0.top.equalTo(enterChosenAuthenticatorLabel.snp.bottom).offset(23)
            $0.height.equalTo(57)
        }
        phoneTextField.snp.makeConstraints {
            $0.left.equalTo(logoImage.snp.left)
            $0.right.equalTo(snp.right).offset(-27)
            $0.top.equalTo(enterChosenAuthenticatorLabel.snp.bottom).offset(23)
            $0.height.equalTo(57)
        }
        verificationDescriptionLabel.snp.makeConstraints {
            $0.top.equalTo(emailTextField.snp.bottom).offset(8)
            $0.left.equalTo(snp.left).offset(46)
            $0.right.equalTo(snp.right).offset(-22)
        }
        continueButton.snp.makeConstraints {
            $0.left.equalTo(snp.left)
            $0.right.equalTo(snp.right)
            $0.top.equalTo(emailTextField.snp.bottom).offset(90)
            $0.height.equalTo(125)
        }
        continueLabel.snp.makeConstraints {
            $0.left.equalTo(snp.left).offset(48)
            $0.top.equalTo(continueButton.snp.top).offset(33)
        }
        pointerImage.snp.makeConstraints {
            $0.top.equalTo(continueButton.snp.top).offset(31)
            $0.right.equalTo(snp.right).offset(-51.5)
        }
        phonePickerView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(390)
            make.top.equalTo(self.snp.bottom)
        }
        showPickerButton.snp.makeConstraints {
            $0.height.equalTo(22)
            $0.width.equalTo(50)
            $0.left.equalTo(emailTextField.snp.left).offset(22)
            $0.top.equalTo(emailTextField.snp.top).offset(18)
        }
        flagPickerView.snp.makeConstraints {
            $0.left.equalToSuperview()
//            $0.top.equalToSuperview()
            $0.size.equalTo(20)
            $0.centerY.equalTo(showPickerButton.snp.centerY)
        }
        flagPointerView.snp.makeConstraints {
            $0.left.equalTo(flagPickerView.snp.right).offset(10)
            $0.centerYWithinMargins.equalTo(flagPickerView.snp.centerYWithinMargins)
        }
    }
    
//    @objc func choosePhoneCode() {
//        emailTextField.isHidden = true
//        phoneTextField.isHidden = false
//        phoneNumberButton.isSelected = true
//        emailButton.isSelected = true
//        phoneNumberButton.image(for: .normal)
//        verificationDescriptionLabel.text = "Verification code will be sent to\nyour phone number"
//        enterChosenAuthenticatorLabel.text = "Enter your phone number"
//        emailTextField.text = "+1"
//        showPickerButton.isHidden = false
//    }
//
//    @objc func chooseEmail() {
//        emailTextField.isHidden = false
//        phoneTextField.isHidden = true
//        phoneNumberButton.isSelected = false
//        emailButton.isSelected = false
//        verificationDescriptionLabel.text = "Verification code will be sent to your e-mail"
//        enterChosenAuthenticatorLabel.text = "Enter your email address"
//        emailTextField.text = ""
//        emailTextField.placeholder = "Example: john.doe@gmail.com"
//        showPickerButton.isHidden = true
//    }
    
    func showPickerView() {
        UIView.animate(withDuration: 0.3) {
            self.phonePickerView.snp.updateConstraints { make in
                make.top.equalTo(self.snp.bottom).offset(-390)
            }
            self.layoutIfNeeded()
        }
    }
    
    func hidePickerView() {
        UIView.animate(withDuration: 0.3) {
            self.phonePickerView.snp.updateConstraints { make in
                make.top.equalTo(self.snp.bottom).offset(0)
            }
            self.layoutIfNeeded()
        }
    }
}