package com.gypsophila.trygank.business.gank.view;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.gypsophila.commonlib.utils.ImageLoaderUtils;
import com.gypsophila.trygank.R;

import java.io.File;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Description :
 * Author : AstroGypsophila
 * Github  : https://github.com/AstroGypsophila
 * Date   : 2016/10/25
 */
public class GankPictureActivity extends SwipeBackActivity {

    public static final String IMG_URL = "IMG_URL";
    public static final String IMG_TITLE = "IMG_TITLE";

    private String mImgUrl;
    private String mImgTitle;
    private ImageView mImageView;
    private Toolbar mToolbar;
    private PhotoViewAttacher mPhotoAttacher;

    private Context mContext;


    public static void openPictureActivity(Context ctx, String imgUrl, String imgTitle) {
        Intent intent = new Intent(ctx, GankPictureActivity.class);
        intent.putExtra(IMG_URL, imgUrl);
        intent.putExtra(IMG_TITLE, imgTitle);
        ctx.startActivity(intent);
    }

    private void parseIntent() {
        mImgUrl = getIntent().getStringExtra(IMG_URL);
        mImgTitle = getIntent().getStringExtra(IMG_TITLE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture);
        mContext = this;
        parseIntent();
        initViews();
    }

    private void initViews() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.inflateMenu(R.menu.picture_menu);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int id = item.getItemId();
                if (id == R.id.picture_save) {
                    ImageLoaderUtils.downloadImage(mContext, mImgUrl, mImgTitle);
                } else if (id == R.id.picture_share) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_SEND);
                    intent.setType("Text/plain");
                    intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.author_github));
                    startActivity(Intent.createChooser(intent, getString(R.string.share_send_to)));
//                    File appDir = new File(Environment.getExternalStorageDirectory(),
//                            getString(R.string.app_name));
//                    if (appDir.exists()) {
//                        String fileName = mImgTitle + ".jpg";
//                        File file = new File(appDir, fileName);
//                        if (file.exists()) {
//                            Uri uri = Uri.fromFile(file);
//                            sharePicture(uri);
//                        } else {
//                            downloadImage();
//                        }
//                    } else {
//                        downloadImage();
//                    }
                }

                return true;
            }
        });
        mImageView = (ImageView) findViewById(R.id.picture);
        ImageLoaderUtils.loadImage(this, mImageView, mImgUrl);
    }


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

    private void sharePicture(Uri uri) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent, getString(R.string.share_send_to)));
    }

    private void downloadImage() {
        ImageLoaderUtils.downloadImage(mContext, mImgUrl, mImgTitle, new ImageLoaderUtils.DownloadImageListener() {
            @Override
            public void loadSuccess(Uri uri) {
                sharePicture(uri);
            }

            @Override
            public void loadFailed(Exception e) {

            }
        });
    }

}

@@ -2585,359 +2585,8 @@
 ) %7B%0A
-                    Intent intent = new Intent();%0A                    intent.setAction(Intent.ACTION_SEND);%0A                    intent.setType(%22Text/plain%22);%0A                    intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.author_github));%0A                    startActivity(Intent.createChooser(intent, getString(R.string.share_send_to)));%0A//
     
@@ -2667,18 +2667,16 @@
 tory(),%0A
-//
         
@@ -2726,18 +2726,16 @@
 name));%0A
-//
         
@@ -2761,34 +2761,32 @@
 Dir.exists()) %7B%0A
-//
                 
@@ -2831,18 +2831,16 @@
 %22.jpg%22;%0A
-//
         
@@ -2891,26 +2891,24 @@
  fileName);%0A
-//
             
@@ -2940,18 +2940,16 @@
 ts()) %7B%0A
-//
         
@@ -2998,18 +2998,16 @@
 (file);%0A
-//
         
@@ -3045,18 +3045,16 @@
 e(uri);%0A
-//
         
@@ -3070,34 +3070,32 @@
        %7D else %7B%0A
-//
                 
@@ -3115,34 +3115,32 @@
 ownloadImage();%0A
-//
                 
@@ -3149,18 +3149,16 @@
       %7D%0A
-//
         
@@ -3178,18 +3178,16 @@
  else %7B%0A
-//
         
@@ -3219,18 +3219,16 @@
 mage();%0A
-//
         

fix gank picture share