package com.breadwallet.presenter.customviews;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.StyleRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.breadwallet.R;

/**
 * BreadWallet
 * <p/>
 * Created by Mihail Gutan on <mihail@breadwallet.com> 3/15/17.
 * Copyright (c) 2017 breadwallet LLC
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
public class BRDialogView extends DialogFragment {

    private String title = "";
    private String message = "";
    private String posButton = "";
    private String negButton = "";
    private BRDialogView.BROnClickListener posListener;
    private BRDialogView.BROnClickListener negListener;
    private DialogInterface.OnDismissListener dismissListener;
    private int iconRes = 0;

    private LinearLayout mainLayout;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.bread_alert_dialog, null);
        TextView titleText = (TextView) view.findViewById(R.id.dialog_title);
        TextView messageText = (TextView) view.findViewById(R.id.dialog_text);
        Button positiveButton = (Button) view.findViewById(R.id.pos_button);
        Button negativeButton = (Button) view.findViewById(R.id.neg_button);
        ImageView icon = (ImageView) view.findViewById(R.id.dialog_icon);

        mainLayout = (LinearLayout) view.findViewById(R.id.main_layout);

        titleText.setText(title);
        messageText.setText(message);
        positiveButton.setText(posButton);
        positiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                posListener.onClick(BRDialogView.this);
            }
        });
        negativeButton.setText(negButton);
        negativeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                negListener.onClick(BRDialogView.this);
            }
        });
        if (iconRes != 0)
            icon.setImageResource(iconRes);

        builder.setView(view);
//        builder.setPositiveButton()
        builder.setOnDismissListener(dismissListener);
        // Create the AlertDialog object and return it
        return builder.create();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setPosButton(String posButton) {
        this.posButton = posButton;
    }

    public void setNegButton(String negButton) {
        this.negButton = negButton;
    }

    public void setPosListener(BRDialogView.BROnClickListener posListener) {
        this.posListener = posListener;
    }

    public void setNegListener(BRDialogView.BROnClickListener negListener) {
        this.negListener = negListener;
    }

    public void setDismissListener(DialogInterface.OnDismissListener dismissListener) {
        this.dismissListener = dismissListener;
    }

    public void setIconRes(int iconRes) {
        this.iconRes = iconRes;
    }

    public static interface BROnClickListener {
        void onClick(BRDialogView brDialogView);
    }

    public void dismissWithAnimation() {
        float scaleX = mainLayout.getScaleX();
        float scaleY = mainLayout.getScaleY();
        mainLayout.animate().scaleX(scaleX / 2);
        mainLayout.animate().scaleY(scaleY / 2).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                BRDialogView.this.dismiss();
            }
        });


    }

}

@@ -5037,24 +5037,26 @@
 imation() %7B%0A
+//
         floa
@@ -5086,24 +5086,26 @@
 etScaleX();%0A
+//
         floa
@@ -5135,24 +5135,26 @@
 etScaleY();%0A
+//
         main
@@ -5190,16 +5190,18 @@
 X / 2);%0A
+//
         
@@ -5280,24 +5280,26 @@
 Adapter() %7B%0A
+//
             
@@ -5300,32 +5300,34 @@
       @Override%0A
+//
             publ
@@ -5367,24 +5367,26 @@
 nimation) %7B%0A
+//
             
@@ -5422,24 +5422,16 @@
 ation);%0A
-        
         
@@ -5455,24 +5455,26 @@
 .dismiss();%0A
+//
             
@@ -5467,32 +5467,34 @@
 //            %7D%0A
+//
         %7D);%0A%0A%0A  

comment the animation for dialog dismiss for now