package ai.pai.client.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;

import ai.pai.client.R;
import ai.pai.client.services.GroupService;
import ai.pai.client.utils.Constants;
import ai.pai.client.utils.PhotoLoader;
import ai.pai.client.utils.SystemUtils;

public class PhotoUploadActivity extends BaseActivity {

    private TextView titleTextView;
    private ImageView uploadPhotoImage;
    private File photoFile;
    private String path;
    private String groupId;

    private static final int TAKE_PHOTO_REQUEST_CODE = 11;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_upload);
        SystemUtils.setImmerseLayout(this,findViewById(R.id.title_layout));
        titleTextView = (TextView)findViewById(R.id.title_bar_middle_txt);
        titleTextView.setText(R.string.photo_upload);
        uploadPhotoImage=(ImageView)findViewById(R.id.iv_photo_upload);
        Bundle bundle = getIntent().getExtras();
        path = bundle.getString("photo_path");
        groupId = bundle.getString("group_id");
        if(!TextUtils.isEmpty(path)){
            PhotoLoader.getInstance(this).displayImage("file://" +path,uploadPhotoImage);
        }

        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        findViewById(R.id.btn_upload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadPhoto();
                backToCamera();
            }
        });

    }

    private void uploadPhoto(){
        if(TextUtils.isEmpty(path)){
            return;
        }

        Intent intent = new Intent(this, GroupService.class);
        Bundle bundle = new Bundle();
        bundle.putInt("command", GroupService.GroupCommand.COMMAND_UPLOAD_PHOTO);
        bundle.putString("group_id",groupId);
        bundle.putString("path",path);
        intent.putExtras(bundle);
        this.startService(intent);
    }

    private void backToCamera(){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File dir = new File(Constants.APP_IMAGE_DIR);
        dir.mkdirs();
        photoFile = new File(dir.getAbsolutePath()+"/"+System.currentTimeMillis()+".jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        startActivityForResult(intent,TAKE_PHOTO_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode== Activity.RESULT_OK && requestCode==TAKE_PHOTO_REQUEST_CODE){
            PhotoLoader.getInstance(this).displayImage("file://" +photoFile.getAbsolutePath(),uploadPhotoImage);
            path = photoFile.getAbsolutePath();
        }else{
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
