//
//  CLLoginMainView.swift

//
//  Created by Leon on 2025/9/29.
//

import UIKit
class CLLoginMainView: UIView {
    var containerView: UIView!
    var iv1: UIImageView!
    var iv2: UIImageView!
    var bgImageViewOverlay: UIImageView!
    
    var bottomFakeBgView: UIView!
    var gradientBottom: UIImageView!
    var bottomView: UIView!
    var button: StyleButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
        initialViews()
        // 延迟启动动画
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) {
            self.setupAnimations()
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func initialViews() {
        let image = UIImage(named: "login_home_bg")
        
        // 计算适合的图片高度
        let fitImageHeight = image!.size.height * UIScreen.main.bounds.width / image!.size.width
        
        containerView = {
            let container = UIView()
            addSubview(container)
            container.snp.makeConstraints { make in
                make.top.leading.trailing.equalToSuperview()
                make.height.equalTo(fitImageHeight * 2)
            }
            return container
        }()
        
        iv1 = {
            let iv = UIImageView()
            iv.image = image
            containerView.addSubview(iv)
            iv.snp.makeConstraints { make in
                make.left.top.right.equalToSuperview()
                make.height.equalTo(fitImageHeight)
            }
            return iv
        }()
        
        iv2 = {
            let iv = UIImageView()
            iv.image = image
            containerView.addSubview(iv)
            iv.snp.makeConstraints { make in
                make.left.right.bottom.equalToSuperview()
                make.top.equalTo(iv1.snp.bottom)
                make.height.equalTo(fitImageHeight)
            }
            return iv
        }()
        
        bgImageViewOverlay = {
            let v = UIImageView()
            let image = UIImage(named: "login_home_bg_overlay")
            v.image = image
            addSubview(v)
            v.snp.makeConstraints { make in
                make.top.leading.trailing.equalToSuperview()
                make.height.equalTo(v.snp.width).multipliedBy(600/393.0)
            }
            return v
        }()

        gradientBottom = {
            let iv = UIImageView()
            iv.image = UIImage(named: "login_home_bottom_overlay")
            addSubview(iv)
            iv.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
                make.bottom.equalToSuperview()
                make.height.equalTo(iv.snp.width).multipliedBy(360/393.0)
            }
            return iv
        }()
        
        bottomFakeBgView = {
            let v = UIView()
            v.backgroundColor = .c.cbd
            insertSubview(v, belowSubview: gradientBottom)
            v.snp.makeConstraints { make in
                make.bottom.leading.trailing.equalToSuperview()
                //make.height.equalTo(218+UIWindow.safeAreaBottom)
                make.top.equalTo(bgImageViewOverlay.snp.bottom)
            }
            return v
        }()

        bottomView = {
            let v = UIView()
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
                make.bottom.equalToSuperview()
            }
            return v
        }()

        button = {
            let button = StyleButton(type: .custom)
            button.primary(size: .large)

            let localizedString = "Login or Signup"
            // let localizedString = NSLocalizedString("welcomeWithName", comment: "")
            button.setTitle(localizedString, for: .normal)

            bottomView.addSubview(button)
            button.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(48)
                make.trailing.equalToSuperview().offset(-48)
                make.bottom.equalToSuperview().offset(-UIWindow.safeAreaBottom - 16)
                make.top.equalToSuperview().offset(24)
            }
            return button
        }()

        let subTitle: UILabel = {
            let label = UILabel()
            label.font = CLSystemToken.font(token: .tll)
            label.textColor = .c.ctpn
            label.textAlignment = .center
            label.numberOfLines = 0
            bottomView.addSubview(label)
            label.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(48)
                make.trailing.equalToSuperview().offset(-48)
                make.bottom.equalTo(button.snp.top).offset(-72)
            }
            return label
        }()
        subTitle.text = "From 'Crush' to 'I Do', Sparked by every chat"
        
        // "Chat, Crush, AI Date"
        
//        let titleLabel: UILabel = {
//            let label = UILabel()
//            label.font = CLSystemToken.font(token: .tdl)
//            label.textColor = .c.ctpn
//            label.textAlignment = .center
//            bottomView.addSubview(label)
//            label.snp.makeConstraints { make in
//                make.leading.equalToSuperview().offset(48)
//                make.trailing.equalToSuperview().offset(-48)
//                make.bottom.equalTo(subTitle.snp.top).offset(-24)
//            }
//            return label
//        }()
//        titleLabel.text = R.string.localizable.crushLevel()
        
        let logo = {
            let v = UIImageView()
            v.image = UIImage(named: "cl_logo")
            bottomView.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.bottom.equalTo(subTitle.snp.top).offset(-24)
            }
            return v
        }()
        logo.isHidden = false
    }
    
    func setupAnimations() {
        let canOff = iv1.frame.height
        containerView.layer.removeAllAnimations()

        let animation = CABasicAnimation(keyPath: "position.y")
        animation.byValue = -canOff
        animation.duration = 20.0
        animation.isRemovedOnCompletion = false
        animation.repeatCount = Float(CGFloat.greatestFiniteMagnitude)
        containerView.layer.add(animation, forKey: "positionAnimation")
    }

    func stopAnimation() {
        containerView.layer.removeAllAnimations()
    }

    func startAnimation() {
        setupAnimations()
    }
    
    deinit {
        stopAnimation()
    }
}
