//
//  ImagePicker.swift
//  LegendTeam
//
//  Created by 梁博 on 13/12/21.
//

import TZImagePickerController
import UIKit

class ImagePicker: TZImagePickerController {
    override init!(maxImagesCount: Int, delegate: TZImagePickerControllerDelegate!) {
        super.init(maxImagesCount: maxImagesCount, columnNumber: 3, delegate: delegate, pushPhotoPickerVc: true)

        let mainColor = UIColor.c.cpn // UIColor.hex120E1B
        let mainTextColor = UIColor.white

        sortAscendingByModificationDate = false
        allowPickingVideo = false
        allowTakePicture = true
        allowCameraLocation = false
        allowTakeVideo = true
        showSelectedIndex = true
        showPhotoCannotSelectLayer = true
        cannotSelectLayerColor = mainColor.withAlphaComponent(0.5)
        modalPresentationStyle = .fullScreen
        statusBarStyle = .lightContent // UIApplication.shared.statusBarStyle

        allowPreview = maxImagesCount > 1

        TZImagePickerConfig.sharedInstance().gifPreviewMaxImagesCount = 1

        #warning("to do")
        naviTitleFont = .t.ttl // UIFont.fredokaOne(size: 18)
        naviBgColor = mainColor
        // normalBgColor = mainColor

        naviTitleColor = mainTextColor
//        naviSubTitleColor = mainTextColor
//        previewTextFont = .t.tbs//UIFont.popSemiBold(size: 12)
//        doneBtnTextFont = .t.tbs//UIFont.popSemiBold(size: 14)

        oKButtonTitleColorNormal = mainTextColor
        oKButtonTitleColorDisabled = .c.ctd // UIColor.hex727085

//        let btnImage = UIImage.gradientHImageWithSize(size: CGSize(width: 80, height: 28), colors: [UIColor.hex8E28FF.cgColor, UIColor.hex3F03FF.cgColor])
//        doneBtnNormalImage = btnImage
//        doneBtnDisableImage = UIColor.hex302F3D.toImage()

        preferredLanguage = "en" // "en"
        doneBtnTitleStr = "Confirm" // R.string.localizable.confirm.localized()
        cancelBtnTitleStr = "Cancel" // R.string.localizable.cancel.localized()
        previewBtnTitleStr = "Preview" // R.string.localizable.preview.localized()
        fullImageBtnTitleStr = "Original Photo" // R.string.localizable.original_photo.localized()

        photoOriginDefImage = R.image.icon_tag_gray_default()
        photoOriginSelImage = R.image.icon_tag_selected()
        photoDefImage = R.image.icon_tag_gray_default()
        photoSelImage = R.image.icon_tag_selected_bg()
        takePictureImage = R.image.icon_album_camera()

        photoPickerPageUIConfigBlock = { (_ collectionView: UICollectionView?,
                                          _ bottomToolBar: UIView?,
                                          _ previewButton: UIButton?,
                                          _ originalPhotoButton: UIButton?,
                                          _ originalPhotoLabel: UILabel?,
                                          _ doneButton: UIButton?,
                                          _: UIImageView?,
                                          _: UILabel?,
                                          _ divideLine: UIView?) in
                collectionView?.backgroundColor = mainColor
                bottomToolBar?.backgroundColor = mainColor
                previewButton?.setTitleColor(mainTextColor, for: .normal)
                previewButton?.setTitleColor(UIColor.gray, for: .disabled)
                originalPhotoButton?.setTitleColor(UIColor.gray, for: .normal)
                originalPhotoButton?.setTitleColor(mainTextColor, for: .selected)
                originalPhotoLabel?.textColor = mainTextColor
                divideLine?.backgroundColor = UIColor.black
//                doneButton?.setBackgroundImage(btnImage, for: .normal)
                doneButton?.backgroundColor = .clear
        }

        photoPreviewPageUIConfigBlock = { (_: UICollectionView?,
                                           _: UIView?,
                                           _: UIButton?,
                                           _: UIButton?,
                                           _: UILabel?,
                                           _: UIView?,
                                           _ originalPhotoButton: UIButton?,
                                           _ originalPhotoLabel: UILabel?,
                                           _: UIButton?,
                                           _: UIImageView?,
                                           _: UILabel?) in
                originalPhotoButton?.setTitleColor(UIColor.gray, for: .normal)
                originalPhotoButton?.setTitleColor(mainTextColor, for: .selected)
                originalPhotoLabel?.textColor = mainTextColor
                //            originalPhotoLabel?.font
        }

        albumCellDidLayoutSubviewsBlock = { (_ cell: TZAlbumCell?,
                                             _: UIImageView?,
                                             _ titleLabel: UILabel?) in

                titleLabel?.font = .t.ttl // UIFont.popSemiBold(size: 16)
                titleLabel?.textColor = .white
                cell?.backgroundColor = mainColor
        }

        setupNoti()
    }

    @available(*, unavailable)
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    // MARK: - noti

    private func setupNoti() {
        NotificationCenter.default.addObserver(self, selector: #selector(notiTzImagePicker(noti:)), name: AppNotificationName.tzImagePickerNoti.notificationName, object: nil)
    }

    @objc private func notiTzImagePicker(noti: Notification) {
        guard let userInfo = noti.userInfo as? Dictionary<String, Any> else { return }

        if let toast = userInfo["toast"] as? String {
            UIWindow.key?.hideToast()
            UIWindow.key?.makeToast(toast)
        }
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}
