//
//  SpeakEleHomeViewCtr.swift
//  SpeakEasyLearnEnglish
//
//  Created by edy on 2025/7/3.
//

import UIKit
import SnapKit

class SpeakEleHomeViewCtr: SpeakEleBaseViewCtr {

    @IBOutlet weak var headerBoard: UIView!
    @IBOutlet weak var SpeakProgressL: UILabel!
    @IBOutlet weak var SpeakEnglistShow: UIButton!
    
    
    lazy var mainScrollview: UIScrollView = {
        let sView = UIScrollView()
        sView.isUserInteractionEnabled = true
        sView.isScrollEnabled = true
        return sView
    }()
    
    lazy var levellb: UILabel = {
        let lLabel = UILabel()
        lLabel.text = "Entry-level"
        lLabel.font = UIFont.montserrat(.regular ,size: 16)
        lLabel.textColor = UIColor.init(with: "#000000", alpha: 0.4)
        return lLabel
    }()
    
    lazy var leftline: UIView = {
       let lineView = UIView()
        lineView.backgroundColor = UIColor.init(with: "#000000", alpha: 0.4)
       return lineView
   }()
    
    lazy var rightline: UIView = {
       let lineV = UIView()
        lineV.backgroundColor = UIColor.init(with: "#000000", alpha: 0.4)
       return lineV
   }()
    
    lazy var topButton: UIButton = {
       let button = UIButton.init(type: .custom)
        button.setImage(UIImage(named: "sp-home-Back-Top"), for: .normal)
        button.addTarget(self, action: #selector(scrollTop), for: .touchUpInside)
       return button
   }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeUI()
        makeLessonUI()
 
    }
//    var index = 10
    // MARK: - 总课程
    var lessonIndex = 21
    
    @IBAction func ShowPro(_ sender: Any) {
        let iap = SpeakEleIAPViewCtr()
        iap.state = .other
        self.navigationController?.AnimationState = .present
        self.navigationController?.pushViewController(iap, animated: true)
    }
    
    @objc private func scrollTop() -> Void {
        UIView.animate(withDuration: 0.2) {
            self.mainScrollview.contentOffset = CGPoint()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        SpeakPermission.requestNotificationPermission()
    }
    
    private func makeUI() {
        self.view.insertSubview(mainScrollview, belowSubview: headerBoard)
        self.view.addSubview(topButton)
        mainScrollview.addSubview(levellb)
        mainScrollview.addSubview(leftline)
        mainScrollview.addSubview(rightline)
        mainScrollview.snp.makeConstraints { make in
            make.top.equalTo(headerBoard.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().inset(Dev.safeAreaInsets.bottom + 50)
        }
        levellb.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(24)
        }
        leftline.snp.makeConstraints { make in
            make.centerY.equalTo(levellb)
            make.right.equalTo(levellb.snp.left).offset(-8)
            make.left.equalTo(16+marginLR)
            make.height.equalTo(0.6)
        }
        rightline.snp.makeConstraints { make in
            make.left.equalTo(levellb.snp.right).offset(8)
            make.centerY.equalTo(levellb)
            make.right.equalTo(self.view.snp.right).offset(-(16+marginLR))
            make.height.equalTo(0.6)
        }
        topButton.snp.makeConstraints { make in
            make.bottom.equalTo(-(tabbarHeight + 26))
            make.right.equalTo(self.view.snp.right).offset(-16)
            make.width.height.equalTo(48)
        }
    }

    func makeLessonUI() {
        let btnW:Double = 96, btnH:Double = 176
        mainScrollview.contentSize = CGSize(width: mainScrollview.bounds.width, height: 2000)
        for num in 0...lessonIndex / 2 {
            let backV = UIImageView()
            let iname = num % 2 == 0 ? "sp-home-xuxianL" : "sp-home-xuxianR"
            backV.image = UIImage(named: iname)
            mainScrollview.addSubview(backV)
            backV.snp.remakeConstraints({ make in
                if num % 2 == 0 {
                    make.right.equalTo(mainScrollview.snp.centerX)
                }else{
                    make.left.equalTo(mainScrollview.snp.centerX)
                }
                make.top.equalToSuperview().offset(Double(num) * 154.adapterW() + 240.adapterH())
                make.width.equalTo(137.adapterW())
                make.height.equalTo(154.adapterW())
            })
        }
        for num in 0...lessonIndex {
            let lview = SpeakEleLessionView()
            lview.setColor(type: num)
            mainScrollview.addSubview(lview)
            if num == 0 {
                lview.snp.remakeConstraints { make in
                    make.centerX.equalToSuperview()
                    make.top.equalToSuperview().offset(240.adapterH() - btnW/2.0)
                    make.size.equalTo(CGSizeMake(btnW, btnH))
                }
            }else if num == 1{
                lview.snp.remakeConstraints { make in
                    make.centerX.equalToSuperview()
                    make.top.equalToSuperview().offset((240.adapterH() - btnW/2.0) + Double(num) * 154.adapterW())
                    make.size.equalTo(CGSizeMake(btnW, btnH))
                }
            }else if num % 4 == 0{
                lview.snp.remakeConstraints { make in
                    make.left.equalToSuperview().offset(14)
                    make.top.equalToSuperview().offset((240.adapterH() - btnW/2.0) + (Double(num)/2.0+0.5) * 154.adapterW())
                    make.size.equalTo(CGSizeMake(btnW, btnH))
                }
            } else if num % 2 == 0{
                lview.snp.remakeConstraints { make in
                    make.centerX.equalToSuperview().offset(154.adapterW() - btnW/2.0 + 10)
                    make.top.equalToSuperview().offset((240.adapterH() - btnW/2.0) + (Double(num)/2.0+0.5) * 154.adapterW())
                    make.size.equalTo(CGSizeMake(btnW, btnH))
                }
            } else {
                lview.snp.remakeConstraints { make in
                    make.centerX.equalToSuperview()
                    make.top.equalToSuperview().offset((240.adapterH() - btnW/2.0) + (Double(num)/2.0+0.5) * 154.adapterW())
                    make.size.equalTo(CGSizeMake(btnW, btnH))
                }
            }
        }
    }
}
