package com.jump.canvasdraw;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CanvasDrawActivity extends Activity implements ColorPickerDialog.OnColorChangedListener {
    private static final String TAG = "CanvasDrawActivity";
    /**
     * Called when the activity is first created.
     */
    private int width;
    private int height;
    private HandWrite handWrite = null;
    private Button clear = null;
    private int whichColor = 0;
    private int whichStrokeWidth = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        handWrite = new HandWrite(this);
        handWrite.setBitmap(null);
        handWrite.setPaint();
        setContentView(handWrite);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        menu.add(0, 1, 1, "清屏");
        menu.add(0, 2, 2, "颜色");
        menu.add(0, 3, 3, "画笔");
        menu.add(0, 4, 4, "保存");
        menu.add(0, 5, 5, "橡皮");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item.getItemId() == 4) {
            File f = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/aaa.jpg");
            try {

                saveMyBitmap(f, handWrite.MergeBitmap());
                scanFile(f.getPath());
                Toast.makeText(this, f.getPath(), Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (item.getItemId() == 1) {
            handWrite.setmCanvas();
        } else if (item.getItemId() == 2) {
            new ColorPickerDialog(this, this, handWrite.getColor()).show();
            handWrite.setStatus(false);
        } else if (item.getItemId() == 3) {
            Dialog mDialog = new AlertDialog.Builder(CanvasDrawActivity.this)
                    .setTitle("画笔设置")
                    .setSingleChoiceItems(new String[]{"细", "中", "粗"}, whichStrokeWidth, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            switch (which) {
                                case 0: {
                                    //画笔的颜色
                                    handWrite.setStrokeWidth(3.0f);
                                    whichStrokeWidth = 0;
                                    break;
                                }
                                case 1: {
                                    //画笔的颜色
                                    handWrite.setStrokeWidth(6.0f);
                                    whichStrokeWidth = 1;
                                    break;
                                }
                                case 2: {
                                    //画笔的颜色
                                    handWrite.setStrokeWidth(9.0f);
                                    whichStrokeWidth = 2;
                                    break;
                                }
                            }
                            handWrite.setPaint();
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                        }
                    })
                    .create();
            mDialog.show();


        } else if (item.getItemId() == 5) {
            handWrite.setStatus(true);
            handWrite.setStrokeWidth(16f);
        }
        return super.onOptionsItemSelected(item);
    }


    public void saveMyBitmap(File f, Bitmap mBitmap) throws IOException {
        try {
            f.createNewFile();
            FileOutputStream fOut;
            fOut = new FileOutputStream(f);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            fOut.flush();
            fOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void scanFile(String path) {//将图片扫描到数据库中
        Uri data_uri = Uri.parse("file://" + path);
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data_uri));
    }

    @Override
    public void colorChanged(int color) {
        handWrite.setColor(color);
    }
}