//
//  Untitled.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/13.
//

import JXSegmentedView
import JXPagingView

class TSTutorialPopupVC: TSBaseVC {
    
    var selectedIndex:Int
    
    init(selectedIndex: Int) {
        self.selectedIndex = selectedIndex
        super.init()
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var tableHeaderViewHeight: CGFloat = 0.0
    var headerInSectionHeight: Int = 44
    let titles:[String] = ["Contact Poster".localized,"Contact Photo".localized]
    let popupContentViewW:CGFloat = k_ScreenWidth - 40.0
    let popupContentViewH:CGFloat = k_ScreenHeight - 268.0
    
    lazy var popupContentView: UIView = {
        let popupContentView = UIView(frame: CGRectMake(0, 0, popupContentViewW, popupContentViewH))
        popupContentView.backgroundColor = .cardColor
        popupContentView.cornerRadius = 20.0
        return popupContentView
    }()
    
    lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.isTitleColorGradientEnabled = true
        dataSource.itemSpacing = 16
        dataSource.itemWidthIncrement = 32
        
        dataSource.isItemSpacingAverageEnabled = false
        dataSource.titles = titles
        dataSource.titleNormalColor = .assist
        dataSource.titleSelectedColor = .white
        dataSource.titleNormalFont = .font(size: 16.0)
        return dataSource
    }()
    
    //配置指示器
    lazy var indicator: JXSegmentedIndicatorBackgroundView = {
        let indicator = JXSegmentedIndicatorBackgroundView()
        indicator.isIndicatorConvertToItemFrameEnabled = true
        indicator.indicatorHeight = 2
        indicator.indicatorPosition = .bottom
        
        let gradientView = JXSegmentedComponetGradientView()
        gradientView.gradientLayer.endPoint = CGPoint(x: 0, y: 1)
        gradientView.gradientLayer.endPoint = CGPoint(x: 1, y: 1)
        gradientView.gradientLayer.colors = ["#E961F6".uiColor.cgColor, "#7E57F4".uiColor.cgColor]
        gradientView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        gradientView.cornerRadius = 1
        indicator.addSubview(gradientView)
        
        return indicator
    }()
    

    lazy var segmentedView: JXSegmentedView = {
        let segmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: popupContentViewW, height: CGFloat(headerInSectionHeight)))
        segmentedView.indicators = [indicator]
        segmentedView.dataSource = segmentedDataSource
        segmentedView.backgroundColor = .clear
        segmentedView.contentEdgeInsetLeft = 26
        segmentedView.listContainer = pagingView.listContainerView
        
        
        let lineView = UIView()
        lineView.backgroundColor = .white.withAlphaComponent(0.2)
        segmentedView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.leading.trailing.equalTo(0)
            make.height.equalTo(1)
        }
        
        return segmentedView
    }()
    
    lazy var pagingView: JXPagingView = {
        let pagingView: JXPagingView = JXPagingListRefreshView(delegate: self) //整个刷新
//        let pagingView = JXPagingView(delegate: self)
        pagingView.mainTableView.backgroundColor = .clear
        pagingView.frame = CGRect(x: 0, y: 25, width:popupContentViewW, height:popupContentViewH-72-25)
        pagingView.listContainerView.listCellBackgroundColor = .clear
        //扣边返回处理，下面的代码要加上
        pagingView.listContainerView.scrollView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        pagingView.mainTableView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        return pagingView
    }()
    
    lazy var confirmBtn: UIButton = {
        let confirmBtn = UIButton.createButton(title: "OK".localized, backgroundColor: "#111111".uiColor,font:.font(size: 18),titleColor: .themeColor,corner: 24){ [weak self]  in
            guard let self = self else { return }
            dismiss()
        }
        return confirmBtn
    }()
    
    override func createView() {
    
        setNavBarViewHidden(true)
        view.backgroundColor = .black.withAlphaComponent(0.7)
        
        contentView.addSubview(popupContentView)
        popupContentView.center = view.center
        createSegmentedView()
        
        popupContentView.addSubview(confirmBtn)
        confirmBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-24)
            make.centerX.equalToSuperview()
            make.width.equalTo(250)
            make.height.equalTo(48)
        }
        
        
        segmentedView.defaultSelectedIndex = selectedIndex
    }

}

extension TSTutorialPopupVC {
    
    func createSegmentedView(){
        popupContentView.addSubview(segmentedView)
        popupContentView.addSubview(pagingView)
    }
}
extension TSTutorialPopupVC: JXPagingViewDelegate {

    func tableHeaderViewHeight(in pagingView: JXPagingView) -> Int {
        return Int(tableHeaderViewHeight)
    }

    func tableHeaderView(in pagingView: JXPagingView) -> UIView {
        return UIView()
    }

    func heightForPinSectionHeader(in pagingView: JXPagingView) -> Int {
        return headerInSectionHeight
    }

    func viewForPinSectionHeader(in pagingView: JXPagingView) -> UIView {
        return segmentedView
    }

    func numberOfLists(in pagingView: JXPagingView) -> Int {
        return titles.count
    }

    func pagingView(_ pagingView: JXPagingView, initListAtIndex index: Int) -> JXPagingViewListViewDelegate {
        return TSTutorialPopupView(selectedIndex: index)
    }
}


@objc public class TSTutorialPopupView: TSBaseView {
    
    var listViewDidScrollCallback: ((UIScrollView) -> ())?
    lazy var stackView: TSCustomStackView = {
        let stackView = TSCustomStackView()
        stackView.scrollView.delegate = self
        stackView.scrollView.isScrollEnabled = false
        return stackView
    }()
    
    var selectedIndex:Int
    init(selectedIndex: Int) {
        self.selectedIndex = selectedIndex
        super.init(frame: .zero)
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    let popupContentView = TSTutorialPopupContentView()

    public override func creatUI() {
        contentView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        stackView.addSubviewToStack(popupContentView)
        popupContentView.snp.makeConstraints { make in
            make.height.equalTo(404*kDesignScale)
        }
    }
    
    public override func dealThings() {
        var imageNamed0 = ""
        var imageNamed1 = ""
        var info = ""
        
        if selectedIndex == 0 {
            imageNamed0 = "tutorial_poster_0"
            imageNamed1 = "tutorial_poster_1"
            info = """
            A new iOS 17 feature that creates a full-screen customizable visual identity for incoming calls. It combines photos, text (name), colors, and fonts, acting like a personalized "wallpaper" for your contact card.
        """.localized
        }else{
            imageNamed0 = "tutorial_photo_0"
            imageNamed1 = "tutorial_photo_1"
            info = """
            A small circular thumbnail image linked to a contact, displayed in apps like Contacts, Messages, or call logs
        """.localized
        }
        
        popupContentView.imageView0.image = UIImage(named: imageNamed0)
        popupContentView.imageView1.image = UIImage(named: imageNamed1)
        

        let attributedString = NSMutableAttributedString(string: info)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 10
        paragraphStyle.alignment = .center
        attributedString.addAttribute(
          .paragraphStyle,
          value: paragraphStyle,
          range: NSRange(location: 0, length: attributedString.length)
        )
        popupContentView.infoLabel.attributedText = attributedString
//        popupContentView.infoLabel.text = info
    }
}

extension TSTutorialPopupView: JXPagingViewListViewDelegate,UIScrollViewDelegate {
    
    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.listViewDidScrollCallback?(scrollView)
    }
    
    public func listScrollView() -> UIScrollView {
        return stackView.scrollView
    }
    
    public func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> ()) {
        listViewDidScrollCallback = callback
    }
    
    public func listView() -> UIView {
        return self
    }
    
}


class TSTutorialPopupContentView: TSBaseView {

    lazy var imageView0: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.cornerRadius = 16
        return imageView
    }()
    
    lazy var imageView1: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.cornerRadius = 16
        return imageView
    }()
    
    lazy var infoLabel: UILabel = {
        let infoLabel = UILabel.createLabel(font: .font(size: 14),textColor: .white.withAlphaComponent(0.8),textAlignment: .center,numberOfLines: 0)
        return infoLabel
    }()
    
    
    
    override func creatUI() {
        
        let centerLineView = UIView()
        centerLineView.backgroundColor = .clear
        
        contentView.addSubview(centerLineView)
        contentView.addSubview(imageView0)
        contentView.addSubview(imageView1)
        contentView.addSubview(infoLabel)
        
        centerLineView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(1)
            make.height.equalTo(1)
        }
        
        imageView0.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.trailing.equalTo(centerLineView.snp.leading).offset(-10)
            make.width.equalTo(98*kDesignScale)
            make.height.equalTo(218*kDesignScale)
        }
        
        imageView1.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.leading.equalTo(centerLineView.snp.trailing).offset(10)
            make.width.equalTo(98*kDesignScale)
            make.height.equalTo(218*kDesignScale)
        }
        
        infoLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView0.snp.bottom).offset(20)
            make.leading.equalTo(20)
            make.bottom.trailing.equalTo(-20)
        }
        
    }
    
}
