//
//  DailyFreeCoinCheckController.swift

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

import UIKit

class DailyFreeCoinCheckController: CLBaseViewController {
    var container: LTScrollContainer!
    
    var header: DialyCheckHeaderView!
    var introduction: DialyCheckIntroductionView!
    var checkDaysView:DailyCheckDaysControl!
    var descLabel : LineSpaceLabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        setupViews()
        setupDatas()
        setupEvents()
    }
    
    private func setupViews() {
        let title = "Dialy Free Crush Coin"
        navigationView.alpha0Title = title
        navigationView.bgView.alpha = 0
        
        container = {
            let v = LTScrollContainer()
            v.scrollView.delegate = self
            v.scrollView.bounces = false
            //view.addSubview(v)
            v.stackEdge = UIEdgeInsets(top: 0, left: 0, bottom: 24+UIWindow.safeAreaBottom, right: 0)
            view.insertSubview(v, at: 0)
            v.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
                make.bottom.equalToSuperview()
                make.top.equalToSuperview()
            }
            return v
        }()
        
        header = {
            let v = DialyCheckHeaderView()
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
            }
            return v
        }()
        container.stack.setCustomSpacing(24, after: header)
        
        introduction = {
            let v = DialyCheckIntroductionView()
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
            }
            return v
        }()
        container.stack.setCustomSpacing(16, after: introduction)
        
        checkDaysView = {
            let v = DailyCheckDaysControl()
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
            }
            return v
        }()
        container.stack.setCustomSpacing(16, after: checkDaysView)
        
        descLabel = {
            let v = LineSpaceLabel()
            v.textColor = .desc
            let typo = CLSystemToken.typography(token:.tbs)
            v.config(typo)
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
            }
            return v
        }()
        descLabel.text = "Diamonds can be used to pay for chats, as well as unlock other props.\nAfter the sign-off is discontinued, you will re-check in from the first day."
    }
    
    private func setupDatas() {
        loadCheckList()
        checkToday()
    }
    
    private func loadCheckList(){
        DiscoverProvider.request(.days7CheckList, modelType: Day7CheckList.self) {[weak self] result in
            Hud.hideIndicator()
            switch result {
            case .success(let model):
                self?.checkDaysView.config(model)
                self?.introduction.config(model)
            case .failure:
                break
            }
        }
    }
    
    private func checkToday(){
        Hud.showIndicator()
        DiscoverProvider.request(.dailyCheck, modelType: EmptyModel.self) {[weak self] result in
            
            switch result {
            case .success:
                self?.loadCheckList()
                Hud.toast(str: "今日已签到")
            case .failure (let failure):
                Hud.hideIndicator()
                switch failure {
                case let .serviceError(code, _):
                    if code == .sign_usernotLoggedIn{
                        // ...
                        self?.close()
                    }
                default:
                    break
                }
            }
        }
    }

    private func setupEvents() {
    }
}

extension DailyFreeCoinCheckController: UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        NaviAlphaHandle.changeNaviViewsAlpha(scrollView: scrollView, alphaViews: [navigationView.titleLabel, navigationView.bgView], oppositeViews: [])
    }
}
