//
//  CreatorIntroductionController.swift

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

import UIKit

class CreatorIntroductionController: CLBaseViewController {
    var scrollContainer: LTScrollContainer!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupViews()
    }

    private func setupViews() {
        let title = "Creator"
        navigationView.alpha0Title = title

        scrollContainer = {
            let v = LTScrollContainer()
            v.stack.spacing = 24
            v.scrollView.delegate = self
            view.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.bottom.equalToSuperview()
                make.top.equalTo(navigationView.snp.bottom)
            }
            return v
        }()

        do {
            let container = UIView()
            scrollContainer.stack.addArrangedSubview(container)
            container.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
            }

            let bg = {
                let v = UIImageView()
                v.image = UIImage(named: "creator_star_bg")
                v.contentMode = .scaleAspectFill
                container.addSubview(v)
                v.snp.makeConstraints { make in
                    make.edges.equalToSuperview()
                }
                return v
            }()
            bg.isHidden = false

            let titleLabel = {
                let v = LineSpaceLabel()
                v.textAlignment = .center
                let typo = CLSystemToken.typography(token: .thm)
                v.config(typo)
                container.addSubview(v)
                v.snp.makeConstraints { make in
                    make.top.equalToSuperview().offset(24)
                    make.leading.equalToSuperview().offset(24)
                    make.trailing.equalToSuperview().offset(-24)
                    make.bottom.equalToSuperview().offset(-24)
                }
                return v
            }()

            do {
                let item = createItemView(iconLeft: true, icon: UIImage(named: "creator-gift"), title: "Gift Revenue", desc: "The interlocutor can pay to send virtual gifts to your AI character.")
                scrollContainer.stack.addArrangedSubview(item)
            }
            do {
                let item = createItemView(iconLeft: false, icon: UIImage(named: "creator-pic"), title: "Image Sales", desc: "The interlocutor can pay to unlock the album pictures you created for the AI character.")
                scrollContainer.stack.addArrangedSubview(item)
            }
            do {
                let item = createItemView(iconLeft: true, icon: UIImage(named: "creator-diamond"), title: "More Revenue", desc: "Stay tuned")
                scrollContainer.stack.addArrangedSubview(item)
            }
            titleLabel.text = "Create high-quality AI characters and win CrushCoin."
        }
    }

    private func createItemView(iconLeft: Bool, icon: UIImage?, title: String, desc: String) -> UIView {
        let container = UIView()

        let stackH = {
            let v = UIStackView()
            v.spacing = 16
            v.axis = .horizontal
            v.distribution = .fillEqually
            v.alignment = .center
            container.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(24)
                make.trailing.equalToSuperview().offset(-24)
                make.top.equalToSuperview().offset(8)
                make.bottom.equalToSuperview().offset(-8)
            }
            return v
        }()

        let wordsStackV = {
            let v = UIStackView()
            v.spacing = 4
            v.axis = .vertical
            v.alignment = .leading
            stackH.addArrangedSubview(v)
            return v
        }()

        let titleLabel = {
            let v = CLLabel()
            v.numberOfLines = 0
            v.font = .t.tts
            wordsStackV.addArrangedSubview(v)
            return v
        }()

        let descLabel = {
            let v = CLLabel()
            v.numberOfLines = 0
            v.font = .t.tbs
            v.textColor = .c.ctsn
            wordsStackV.addArrangedSubview(v)
            return v
        }()

        let iconView = {
            let v = UIImageView()
            v.backgroundColor = .c.cbdi
            v.cornerRadius = 16
            if iconLeft {
                stackH.insertArrangedSubview(v, at: 0)
            } else {
                stackH.addArrangedSubview(v)
            }
            v.snp.makeConstraints { make in
                make.height.equalTo(v.snp.width).multipliedBy(107 / 160.0)
            }
            return v
        }()

        titleLabel.text = title
        descLabel.text = desc
        iconView.image = icon

        return container
    }
}

extension CreatorIntroductionController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        NaviAlphaHandle.changeNaviTitleAlpha(scrollView: scrollView, titleLabel: navigationView.titleLabel)
    }
}
