package com.github.rubensousa.previewseekbar;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.List;

public class PreviewSeekBarLayout extends LinearLayoutCompat {

    private PreviewDelegate delegate;
    private PreviewSeekBar seekBar;
    private PreviewFrameLayout previewFrameLayout;
    private View morphView;
    private View frameView;
    private boolean firstLayout = true;

    public PreviewSeekBarLayout(Context context) {
        super(context);
        init(context, null);
    }

    public PreviewSeekBarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public PreviewSeekBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedValue outValue = new TypedValue();

        getContext().getTheme().resolveAttribute(R.attr.colorAccent, outValue, true);
        int colorRes = outValue.resourceId;

        // Create morph view
        morphView = new View(getContext());
        morphView.setBackgroundResource(R.drawable.previewseekbar_morph);

        // Tint to accent color
        Drawable drawable = morphView.getBackground();
        int colorInt = ContextCompat.getColor(getContext(), colorRes);
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(drawable, colorInt);
        morphView.setBackground(drawable);

        // Create frame view for the circular reveal
        frameView = new View(getContext());
        frameView.setBackgroundResource(colorRes);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (getWidth() == 0 || getHeight() == 0) {
            return;
        } else if (firstLayout) {

            // Check if we have the proper views
            if (!checkChilds()) {
                throw new IllegalStateException("You need to add a PreviewSeekBar" +
                        "and a PreviewFrameLayout as direct childs");
            }

            delegate = new PreviewDelegate(this);

            // Setup morph view
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(0, 0);
            layoutParams.width = getResources()
                    .getDimensionPixelSize(R.dimen.previewseekbar_indicator_width);
            layoutParams.height = layoutParams.width;
            addView(morphView, layoutParams);

            // Add frame view to the preview layout
            FrameLayout.LayoutParams frameLayoutParams
                    = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            frameLayoutParams.gravity = Gravity.CENTER;
            previewFrameLayout.addView(frameView, frameLayoutParams);
            firstLayout = false;
        }
    }


    public PreviewFrameLayout getPreviewFrameLayout() {
        return previewFrameLayout;
    }

    public PreviewSeekBar getSeekBar() {
        return seekBar;
    }

    View getFrameView() {
        return frameView;
    }

    View getMorphView() {
        return morphView;
    }

    private boolean checkChilds() {
        int childs = getChildCount();

        if (childs != 2) {
            return false;
        }

        View firstChild = getChildAt(0);
        View secondChild = getChildAt(1);

        if (firstChild instanceof PreviewSeekBar) {
            seekBar = (PreviewSeekBar) firstChild;
            if (secondChild instanceof PreviewFrameLayout) {
                previewFrameLayout = (PreviewFrameLayout) secondChild;
                return true;
            }
        } else if (secondChild instanceof PreviewSeekBar) {
            seekBar = (PreviewSeekBar) secondChild;
            if (firstChild instanceof PreviewFrameLayout) {
                previewFrameLayout = (PreviewFrameLayout) firstChild;
                return true;
            }
        }

        return false;
    }

}

@@ -228,104 +228,8 @@
 at;%0A
-import android.support.v4.view.ViewCompat;%0Aimport android.support.v7.widget.LinearLayoutCompat;%0A
 impo
@@ -415,57 +415,43 @@
 ut;%0A
-%0A
 import 
-java.util.ArrayList;%0Aimport java.util.Lis
+android.widget.RelativeLayou
 t;%0A%0A
@@ -496,27 +496,22 @@
 nds 
-LinearLayoutCompat 
+RelativeLayout
 %7B%0A%0A 
@@ -3737,10 +3737,9 @@
 lds 
-!=
+%3C
  2) 
@@ -3789,25 +3789,104 @@
     
-View firstChild =
+boolean hasSeekbar = false;%0A        boolean hasFrameLayout = false;%0A%0A        for (int i = 0; i %3C
  get
@@ -3894,15 +3894,28 @@
 hild
-A
+Coun
 t(
-0
 );
-%0A
+ i++) %7B%0A    
     
@@ -3923,23 +3923,17 @@
    View 
-secondC
+c
 hild = g
@@ -3946,13 +3946,17 @@
 dAt(
-1
+i
 );%0A%0A
+    
     
@@ -3955,38 +3955,33 @@
             if (
-firstC
+c
 hild instanceof 
@@ -3990,32 +3990,71 @@
 eviewSeekBar) %7B%0A
+                hasSeekbar = true;%0A    
             seek
@@ -4076,22 +4076,17 @@
 eekBar) 
-firstC
+c
 hild;%0A  
@@ -4095,27 +4095,28 @@
         
-if (secondC
+%7D else if (c
 hild ins
@@ -4203,23 +4203,17 @@
 Layout) 
-secondC
+c
 hild;%0A  
@@ -4218,38 +4218,48 @@
                 
-return
+hasFrameLayout =
  true;%0A         
@@ -4263,24 +4263,25 @@
       %7D%0A
+%0A
         
 %7D else i
@@ -4276,241 +4276,47 @@
     
-%7D else if (secondChild instanceof PreviewSeekBar) %7B%0A            seekBar = (Preview
+    if (has
 Seek
-Bar) secondChild;%0A            if (firstChild instanceof PreviewFrameLayout) %7B%0A                previewFrameLayout = (PreviewFrameLayout) firstChild;
+bar && hasFrameLayout) %7B
 %0A   
@@ -4377,28 +4377,51 @@
      return 
-false
+hasSeekbar && hasFrameLayout
 ;%0A    %7D%0A%0A%7D%0A

Extend from RelativeLayout due to limitations