package com.ifavine.pay.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.ifavine.pay.R;
import com.ifavine.pay.listener.OnMenuClickListener;

import java.io.File;

public class PickPhotoMenuDialog extends Dialog implements View.OnClickListener {

    public final static int CAMERA = 1;
    public final static int ALBUMS = 2;
    private Context mContext;

    private OnMenuClickListener mListener;

    private PickPhotoMenuDialog(Context context, boolean cancelable,
        OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    private PickPhotoMenuDialog(Context context, int defStyle) {
        super(context, defStyle);
        View view = getLayoutInflater().inflate(R.layout.dialog_photo_picker, null);
        view.findViewById(R.id.tv_take_photo).setOnClickListener(this);
        view.findViewById(R.id.tv_take_album).setOnClickListener(this);
        view.findViewById(R.id.tv_cancel).setOnClickListener(this);
        super.setContentView(view);
    }

    public PickPhotoMenuDialog(Context context) {
        this(context, R.style.dialog_bottom);
        this.mContext = context;
    }

    @Override protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().setGravity(Gravity.BOTTOM);

        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = d.getWidth();
        getWindow().setAttributes(p);
    }

    public void setOnMenuClickListener(OnMenuClickListener listener) {
        mListener = listener;
    }


    @Override public void onClick(View v) {
        if (mListener != null) {
            mListener.onClick((TextView) v);
        }
    }

    // 照像添加图片
    public void getImageFromCamera(Activity activity, String filePath) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath)));
        activity.startActivityForResult(intent, CAMERA);
    }

    // 从相册添加图片
    public void getImageFormAlbum(Activity activity) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        activity.startActivityForResult(intent, ALBUMS);
    }
}