//
//  LNProfileInfosView.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/30.
//

import Foundation
import UIKit
import SnapKit


class LNProfileInfosView: UIView {
    private var heightConstraint: Constraint?
    private let tabView = LNProfileTabView()
    private let scrollView = UIScrollView()
    
    private var detailView = LNProfileUserDetailView()
    private let photoWall = LNProfilePhotoWall()
    
    var maxHeight: CGFloat = 0.0 {
        didSet {
            updateViewHeight()
        }
    }
    var minHeight: CGFloat = 0.0 {
        didSet {
            updateViewHeight()
        }
    }
    var progress: CGFloat = 0.0 {
        didSet {
            layer.cornerRadius = 20 * (1 - progress.bounded(min: 0, max: 1.0))
        }
    }
    
    weak var outerScrollView: LNNestedScrollViewDelegate? {
        didSet {
            detailView.outerScrollView = outerScrollView
            photoWall.outerScrollView = outerScrollView
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        
        tabView.curSelected = .info
    }
    
    func update(_ detail: LNUserProfileVO) {
        detailView.update(detail)
        photoWall.update(detail.photos)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNProfileInfosView: LNProfileTabViewDelegate {
    func onProfileTabView(view: LNProfileTabView, didSelect at: Int, type: LNProfileTabType) {
        scrollView.setContentOffset(.init(x: CGFloat(at) * scrollView.bounds.width, y: 0), animated: true)
    }
}

extension LNProfileInfosView: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = scrollView.contentOffset.x / scrollView.bounds.width
        if page == 0 {
            tabView.curSelected = .info
        } else {
            tabView.curSelected = .photo
        }
    }
    
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        let page = scrollView.contentOffset.x / scrollView.bounds.width
        if page == 0 {
            tabView.curSelected = .info
        } else {
            tabView.curSelected = .photo
        }
    }
}

extension LNProfileInfosView: LNProfilePhotoWallDelegate {
    func onProfilePhotoWall(view: LNProfilePhotoWall, contentHeightChanged height: CGFloat) {
        updateViewHeight()
    }
}

extension LNProfileInfosView: LNProfileUserDetailViewDelegate {
    func onProfileUserDetailView(view: LNProfileUserDetailView, contentHeightChanged height: CGFloat) {
        updateViewHeight()
    }
}

extension LNProfileInfosView {
    private func updateViewHeight() {
        var height = max(detailView.contentHeight, photoWall.contentHeight) + tabView.bounds.height
        height = height.bounded(min: minHeight, max: maxHeight)
        heightConstraint?.update(offset: height)
        if height > 0 {
            heightConstraint?.update(priority: .high)
        }
    }
    
    private func setupViews() {
        snp.makeConstraints { make in
            heightConstraint = make.height.equalTo(0).priority(.low).constraint
        }
        backgroundColor = .fill
        layer.cornerRadius = 20
        layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        
        tabView.delegate = self
        addSubview(tabView)
        tabView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        scrollView.isPagingEnabled = true
        scrollView.backgroundColor = .fill
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(tabView.snp.bottom)
            make.bottom.equalToSuperview()
        }
        
        let fakeView = UIView()
        scrollView.addSubview(fakeView)
        fakeView.snp.makeConstraints { make in
            make.leading.top.bottom.equalToSuperview()
            make.height.equalToSuperview()
            make.width.equalTo(0)
        }
        
        let intro = buildIntroduce()
        scrollView.addSubview(intro)
        intro.snp.makeConstraints { make in
            make.leading.verticalEdges.equalToSuperview()
            make.width.equalToSuperview()
        }
        
        let photo = buildPhotoWall()
        scrollView.addSubview(photo)
        photo.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.verticalEdges.equalToSuperview()
            make.leading.equalTo(intro.snp.trailing)
            make.width.equalToSuperview()
        }
    }
    
    private func buildIntroduce() -> UIView {
        detailView.delegate = self
        return detailView
    }
    
    private func buildPhotoWall() -> UIView {
        photoWall.delegate = self
        
        return photoWall
    }
}
