//
//  DailyFreenCoinCheckPieces.swift

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

class DialyCheckHeaderView: UIView {
    var imageView: UIImageView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews() {
        // backgroundColor = .random

        imageView = {
            let v = UIImageView()
            v.image = UIImage(named: "daily_check_bg_banner")
            addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
                make.height.equalTo(v.snp.width).multipliedBy(248 / 393.0)
            }
            return v
        }()
    }
}

class DialyCheckIntroductionView: UIView {
    var label: LineSpaceLabel!
    var lineIv: UIImageView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews() {
//        backgroundColor = .random

        label = {
            let v = LineSpaceLabel()
            v.textColor = .text
            v.textAlignment = .center
            let typo = CLSystemToken.typography(token: .ttm)
            v.config(typo)

            addSubview(v)
            v.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(8)
                make.leading.equalToSuperview().offset(24)
                make.trailing.equalToSuperview().offset(-24)
                // make.bottom.equalToSuperview().offset(-40)
            }
            return v
        }()

        lineIv = {
            let v = UIImageView()
            v.image = UIImage(named: "daily_check_line")
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(24)
                make.trailing.equalToSuperview()
                make.top.equalTo(label.snp.bottom).offset(0)
                make.bottom.equalToSuperview()
                make.height.equalTo(v.snp.width).multipliedBy(88 / 345.0)
            }
            return v
        }()

        label.text = "You have checked in for 1 consecutive days"
    }
    
    func config(_ checkList: Day7CheckList?) {
        guard let checkData = checkList, let days = checkData.continuousDays else { return }
        
        label.text = "You have checked in for \(days) consecutive days"
    }
}

class DailyCheckDaysControl: UIView {
    var stackV: UIStackView!
    var row1: UIStackView!
    var row2: UIStackView!

    // row 1
    var day1: DailyCheckDayView!
    var day2: DailyCheckDayView!
    var day3: DailyCheckDayView!

    // row 2
    var day4: DailyCheckDayView!
    var day5: DailyCheckDayView!
    var day6: DailyCheckDayView!

    var day7: DailyCheckDay7View!

    var daysItems = [DailyCheckDayView]()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        setupData()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews() {
        stackV = {
            let v = UIStackView()
            v.spacing = 12
            v.axis = .vertical
            //            v.alignment = .center
            addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()

        row1 = {
            let v = UIStackView()
            v.spacing = 8
            v.distribution = .fillEqually
            v.alignment = .center
            stackV.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.height.equalTo(120)
            }
            return v
        }()

        row2 = {
            let v = UIStackView()
            v.spacing = 8
            v.distribution = .fillEqually
            v.alignment = .center
            stackV.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.height.equalTo(120)
            }
            return v
        }()

        day1 = {
            let v = DailyCheckDayView()
            row1.addArrangedSubview(v)
            return v
        }()

        day2 = {
            let v = DailyCheckDayView()
            row1.addArrangedSubview(v)
            return v
        }()

        day3 = {
            let v = DailyCheckDayView()
            row1.addArrangedSubview(v)
            return v
        }()

        day4 = {
            let v = DailyCheckDayView()
            row2.addArrangedSubview(v)
            return v
        }()

        day5 = {
            let v = DailyCheckDayView()
            row2.addArrangedSubview(v)
            return v
        }()

        day6 = {
            let v = DailyCheckDayView()
            row2.addArrangedSubview(v)
            return v
        }()

        day7 = {
            let v = DailyCheckDay7View()
            stackV.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.height.equalTo(120)
            }
            return v
        }()

        daysItems.append(day1)
        daysItems.append(day2)
        daysItems.append(day3)
        daysItems.append(day4)
        daysItems.append(day5)
        daysItems.append(day6)
        daysItems.append(day7)
    }

    private func setupData() {
        day1.setupDay(1)
        day2.setupDay(2)
        day3.setupDay(3)
        day4.setupDay(4)
        day5.setupDay(5)
        day6.setupDay(6)
        day7.setupDay(7)
    }

    // MARK: Public

    func config(_ checkList: Day7CheckList?) {
        guard let checkData = checkList, let daysList = checkData.list else { return }

        for (index, per) in daysList.enumerated() {
            let dayItem = daysItems[index]
            dayItem.setupCoin(per.coinNum ?? 0)
            dayItem.setupCheckedTheDay(per.signIn.boolValue)
        }
    }

//    func setupCheckedTheDay(_ checked: Bool) {
//        day1.setupCheckedTheDay(checked)
//        day2.setupCheckedTheDay(checked)
//        day3.setupCheckedTheDay(false)
//        day4.setupCheckedTheDay(false)
//        day5.setupCheckedTheDay(false)
//        day6.setupCheckedTheDay(false)
//        day7.setupCheckedTheDay(false)
//    }
}

class DailyCheckDayView: UIView {
    /// 未签到时，会亮起的主题色背景
    var gradidentBg: GradientView!
    var imageViewBg: UIImageView!

    var dayFlagContainer: UIView!
    var dayLabel: CLLabel!

    var coinAndLabel: CLIconLabel!

    var checkedStackH: UIStackView!
    //var checkButton: StyleButton!
    var notStartLabel : CLLabel!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupViews() {
        cornerRadius = 16
        layer.borderColor = UIColor.c.con.cgColor
        layer.borderWidth = 1

        gradidentBg = {
            let gradient = CLSystemToken.gradient(token: .cpgn)
            let v = GradientView(colors: gradient.colors(), gradientType: .leftToRight)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()

        imageViewBg = {
            let v = UIImageView()
            v.image = UIImage(named: "daily_check_bg_diamond")
            addSubview(v)
            v.snp.makeConstraints { make in
                // make.edges.equalToSuperview()
                make.trailing.top.bottom.equalToSuperview()
                make.height.equalTo(v.snp.width).multipliedBy(120 / 140.0)
            }
            return v
        }()

        dayFlagContainer = {
            let v = UIView()
            v.backgroundColor = .c.cseln
            v.layer.cornerRadius = 16
            v.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMaxYCorner]
            addSubview(v)
            v.snp.makeConstraints { make in
                make.top.leading.equalToSuperview()
                make.height.equalTo(28)
            }
            return v
        }()

        dayLabel = {
            let v = CLLabel()
            v.font = .t.tls
            dayFlagContainer.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.leading.equalToSuperview().offset(8)
                make.trailing.equalToSuperview().offset(-8)
            }
            return v
        }()

        coinAndLabel = {
            let v = CLIconLabel()
            v.iconImageView.image = UIImage(named: "icon_32_diamond")
            v.iconSize = CGSizeMake(24, 24)
            v.contentLabel.font = .t.tndm
            v.contentLabel.text = "-"
            addSubview(v)
            v.snp.makeConstraints { make in
                make.center.equalToSuperview()
            }
            return v
        }()

        checkedStackH = {
            let v = UIStackView()
            v.spacing = 4
            v.alignment = .center
            addSubview(v)
            v.snp.makeConstraints { make in
                make.height.equalTo(20)
                make.centerX.equalToSuperview()
                make.bottom.equalToSuperview().offset(-10)
            }

            let icon = UIImageView()
            icon.image = UIImage(named: "radio_checkbox_unselected")
            icon.snp.makeConstraints { make in
                make.size.equalTo(CGSize(width: 20, height: 20))
            }
            v.addArrangedSubview(icon)

            let label = CLLabel()
            label.font = .t.tls
            label.text = "Checked"
            v.addArrangedSubview(label)
            v.isHidden = true
            return v
        }()

//        checkButton = {
//            let v = StyleButton()
//            v.contrastTertiaryDark(size: .small)
//            v.setTitle("Check in", for: .normal)
//            addSubview(v)
//            v.snp.makeConstraints { make in
//                make.centerX.equalToSuperview()
//                make.bottom.equalToSuperview().offset(-4)
//            }
//            return v
//        }()
        
        notStartLabel = {
            let v = CLLabel()
            v.font = .t.tls
            addSubview(v)
            v.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.bottom.equalToSuperview().offset(-10)
            }
            return v
        }()

//        #warning("test")
//        testData()
        
        notStartLabel.text = "Not Started"
    }

    private func testData() {
        setupDay(1)
        coinAndLabel.contentLabel.text = "5"
        setupCheckedTheDay(true)
    }

    // MARK: - Public
    func setupCoin(_ coin: Int){
        let coin = Coin(cents: coin)
        coinAndLabel.contentLabel.text = coin.formatted
    }

    func setupCheckedTheDay(_ checked: Bool) {
        if checked {
            layer.borderColor = UIColor.c.con.cgColor
            gradidentBg.isHidden = true
            checkedStackH.isHidden = false
            notStartLabel.isHidden = true
        } else {
            layer.borderColor = CLGlobalToken.color(token: .glo_color_orange_10)?.cgColor
            gradidentBg.isHidden = false
            checkedStackH.isHidden = true
            notStartLabel.isHidden = false
        }
    }

    func setupDay(_ whichDay: Int) {
        dayLabel.text = "Day \(whichDay)"
    }
}

class DailyCheckDay7View: DailyCheckDayView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        // setupViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setupViews() {
        super.setupViews()

        imageViewBg.snp.remakeConstraints { make in
            make.leading.equalToSuperview().offset(92)
            make.top.bottom.equalToSuperview()
            make.height.equalTo(imageViewBg.snp.width).multipliedBy(120 / 140.0)
        }

        coinAndLabel.snp.remakeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(imageViewBg)
        }
        
        notStartLabel.snp.remakeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-28)
        }

        checkedStackH.snp.remakeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
        }
    }
}
