package com.usai.redant.CommonEditor;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.usai.redant.redantmobile.R;

public class SignatureActivity extends AppCompatActivity {


    public static class Builder {
        public Intent build(Context context,int group, int child) {
            Intent intent = new Intent(context,SignatureActivity.class);

            intent.putExtra("group",group);
            intent.putExtra("child",child);

            return intent;
        }
    }

    private BrushView brushView;
    private int group,child;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signature);

        brushView = (BrushView) findViewById(R.id.brush_view);

//        brushView = new BrushView(this);
//        setContentView(brushView);

        Intent intent = getIntent();
        group = intent.getIntExtra("group",0);
        child = intent.getIntExtra("child",0);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.signature_action_bar_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.save_signature_item: {
                if (brushView.checkCanSave()) {
                    Bitmap bitmap = brushView.getDrawBitmap();
                    String path = Utils.cacheTmpImage(bitmap);

                    // upload signature

                    // upload success
                    Intent intent = new Intent();
                    intent.putExtra("url_down",path);
                    intent.putExtra("url_up",path);
                    intent.putExtra("path",path);
                    intent.putExtra("group",group);
                    intent.putExtra("child",child);

                    setResult(RESULT_OK,intent);
                    finish();
                } else {

                    new AlertDialog.Builder(this)
                            .setTitle("Warning")
                            .setMessage("Please Sign")
                            .setPositiveButton("Ok",null)
                            .show();

                }
            }
            break;
            case R.id.clear_signature_item: {
                brushView.clear();
            }
            break;
        }

        return true;
    }
}
