 GroupLogoSettings  = require '../grouplogosettings'
 KDNotificationView = kd.NotificationView
 
-
 module.exports = class GroupGeneralSettingsView extends KDView
 
   constructor: (options = {}, data) ->

remote             = require('app/remote').getInstance()
KDView             = kd.View
Encoder            = require 'htmlencode'
showError          = require 'app/util/showError'
geoPattern         = require 'geopattern'
KDFormView         = kd.FormView

    section.addSubView @avatar = new KDCustomHTMLView
      cssClass : 'avatar'

    section.addSubView @uploadButton = new KDButtonView
      cssClass : 'compact solid green upload'
      title    : 'UPLOAD IMAGE'
      loader   : yes

    section.addSubView @uploadInput = new KDInputView
      type       : 'file'
      cssClass   : 'upload-input'
      attributes : accept : 'image/*'
      change     : @bound 'handleUpload'

    @showAvatar()


  handleUpload: ->

    @uploadButton.showLoader()

    [file] = @uploadInput.getElement().files

    return @uploadButton.hideLoader()  unless file

    reader        = new FileReader
    reader.onload = (event) =>
      options     =
        mimeType  : file.type
        content   : file

      @uploadAvatar options, => @uploadAvatarBtn.hideLoader()

    reader.readAsDataURL file


  uploadAvatar: (fileOptions, callback) ->

    { mimeType, content } = fileOptions
    group   = kd.singletons.groupsController.getCurrentGroup()
    name    = "#{group.slug}-logo-#{Date.now()}.png"
    timeout = 3e4

    s3upload { name, content, mimeType, timeout }, (err, url) =>

      @uploadButton.hideLoader()

      return showError err   if err

      group.modify { 'customize.logo': url }, =>
        @showAvatar url


  showAvatar: (url) ->

    avatarEl = @avatar.getElement()
    jGroup   = @getData()
    url    or= jGroup.customize?.logo

    if url
      avatarEl.style.backgroundImage = "url(#{url})"
    else
      pattern = geoPattern.generate jGroup.title, generator: 'plusSigns'

