package de.beyondsoft.ownchat.page.fileconfirmation;

import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.ImageView;

import de.beyondsoft.ownchat.R;
import de.beyondsoft.ownchat.di.InjectionHelper;
import de.beyondsoft.ownchat.utils.Constants;
import de.beyondsoft.ownchat.utils.FileUtils;

import java.io.File;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import hani.momanii.supernova_emoji_library.Helper.EmojiconEditText;

public class FileConfirmationActivity extends AppCompatActivity implements FileConfirmationView {

    @BindView(R.id.file_message)
    EmojiconEditText mMessageEmojiconEditText;

    @BindView(R.id.file_emoji_button)
    ImageView mEmojiButtonImageView;

    @BindView(R.id.media_file_webview)
    WebView mFileWebView;

    @BindView(R.id.file_send_message)
    ImageView mSendMessageButtonImageView;


    @Inject
    FileConfirmationPresenter mFileConfirmationPresenter;

    private Callback mCallback;

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

        InjectionHelper.getFileConfirmationComponent(this).inject(this);
        ButterKnife.bind(this);
        mFileConfirmationPresenter.attachView(this);

        String fileUri = getIntent().getStringExtra(Constants.FILENAME);

        File file = getFile(Uri.parse(fileUri));
        String absolutePath = file.getAbsolutePath();

        String html = "<html><head></head><body><img src=\""+ absolutePath + "\"></body></html>";
        try {
            mFileWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getPath(Uri uri) {
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

        if(cursor == null) {
            return null;
        }

        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(columnIndex);
        cursor.close();

        return path;
    }

    @Override
    public void sendFileSuccessful() {

    }

    @Override
    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    @Override
    protected void onDestroy() {
        InjectionHelper.destroyLoginComponent();
        mFileConfirmationPresenter.detachView();
        super.onDestroy();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void showContent() {

    }

    @Override
    public void showError(@StringRes int messageRes) {

    }

    @Override
    public void showError(String message) {

    }

    public File getFile(Uri fileUri) {
        return FileUtils.getFile(this, fileUri);
    }
}
