(in-package :om)



;;;;;;;;;;;;;;;;;;;;;;;;;

;;;first get all notes and their continuation

(defmethod grp-cont-chords ((self list))
  (let ((res '()))
    (loop for i in self
          do (cond
              ((rest-p i) (push (list i) res))
              ((and (chord-p i) (not (cont-chord-p i)))
               (push  (list i)  res))
               (t (push i (car res))
                  )))
   (reverse  (mapcar 'reverse res))))
           
(defmethod grp-cont-notes ((self voice))
  (let* ((objs (collect-chords self))
         (grpchrds (grp-cont-chords objs)))
    (loop for i in grpchrds
          collect (cond
                   ((> (length i) 1) (mat-trans (mapcar 'inside i)))
                   ((chord-p (car i)) (inside (car i)))
                   (t  i)))))
    

(defmethod set-notes-color ((self voice) (pos list) color)
  (let* ((notes (flat-once (grp-cont-notes self))))
    (loop for i in (posn-match notes pos)
          do (if (listp i) 
                 (mapcar #'(lambda (x) (set-mus-color x color)) i)
               (set-mus-color i color)))))
          

;;; now if not colored (= (make-color 0 0 0))  if not --> grey

(defun black-color-p (c1)
  (let ((col1 (format nil "~a" c1)))
        (string-equal col1 "color R:0 G:0 B:0")))

;;init color black before
(defmethod init-def-color-notes ((self simple-container))
  (set-mus-color self (om-make-color 0 0 0)))

(defmethod init-def-color-notes ((self container))
 (loop for i in (inside self)
         do (init-def-color-notes i)))
  

(defmethod set-notes-color-if ((notes list) (pos list) color)
  (loop for i in (posn-match notes pos)
        do (if (listp i) 
               (mapcar #'(lambda (x) (if (black-color-p (mus-color x))
                                         (set-mus-color x color)
                                       (set-mus-color x (om-make-color 0.8 0.8 0.8)))) i)
             (if (black-color-p (mus-color i))
                 (set-mus-color i color)
               (set-mus-color i (om-make-color 0.8 0.8 0.8))
               ))))


(defmethod! set-color-analysis ((self voice) (positions list) (colors list))
   :icon 355
   :indoc '("voice" "list of positions" "list of omcolors")
   :initvals (list t t t)
   :doc "Sets colors of NOTE and REST objects"
   (init-def-color-notes self)
  (let* ((notes (flat-once (grp-cont-notes self))))
    (loop for i in positions
          for c in colors
          do (set-notes-color-if notes i c))))

