/*
 * Copyright (c) 2017 Diona Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of Diona
 * ("Confidential Information"). You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement you
 * entered into with Diona.
 */

package org.odk.collect.android.views.colorpicker;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SeekBar;

import org.odk.collect.android.R;

public class OpacityPicker extends OrientedSeekBar {

  private static final int MIN_SIZE_DIP = 200;
  private static final int MAX_OPACITY = 255;

  private static int minSizePx;
  Context mContext;
  private OnOpacityPickedListener onOpacityPickedListener;
  private boolean canUpdateHexVal = true;


  public OpacityPicker(Context context) {
    super(context);
    init(context);
  }

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

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OpacityPicker, 0, 0);

    try {
      if (a.getBoolean(R.styleable.OpacityPicker_cp_showOpacityBar, true)) {
        setVisibility(View.VISIBLE);
      } else {
        setVisibility(View.GONE);
      }
    } finally {
      a.recycle();
    }
  }


  private void init(Context context) {
    minSizePx = (int) Stools.dipToPixels(context, MIN_SIZE_DIP);
    mContext = context;
    setMax(MAX_OPACITY);

    setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        setOp(progress);
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {

      }

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {

      }
    });


  }

  public void setOnOpacityPickedListener(OnOpacityPickedListener onOpacityPickedListener) {
    this.onOpacityPickedListener = onOpacityPickedListener;
  }

  public void setOp(int opacity) {
    if (onOpacityPickedListener != null) {
      onOpacityPickedListener.onPicked(opacity);
    }
  }

  @Override
  public synchronized void setProgress(int progress) {
    super.setProgress(progress);
//        setOp(progress);
  }

  public boolean isCanUpdateHexVal() {
    return canUpdateHexVal;
  }

  public void setCanUpdateHexVal(boolean canUpdateHexVal) {
    this.canUpdateHexVal = canUpdateHexVal;
  }

  public interface OnOpacityPickedListener {
    void onPicked(int opacity);
  }
}