package de.beyondsoft.ownchat.page.fileconfirmation;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.DrawableImageViewTarget;
import com.bumptech.glide.request.target.Target;

import java.io.File;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.beyondsoft.ownchat.R;
import de.beyondsoft.ownchat.di.InjectionHelper;
import de.beyondsoft.ownchat.model.Group;
import de.beyondsoft.ownchat.utils.Constants;
import de.beyondsoft.ownchat.utils.ui.GlideApp;
import de.beyondsoft.ownchat.utils.ui.ViewUtils;
import hani.momanii.supernova_emoji_library.Actions.EmojIconActions;
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)
    ImageView mFileImageView;

    @BindView(R.id.file_relative_layout)
    RelativeLayout mRelativeLayout;

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

    @BindView(R.id.chat_toolbar_image_view)
    ImageView mChatImageView;

    @BindView(R.id.chat_toolbar_single_title)
    TextView mSingleChatTitleTextView;

    @BindView(R.id.chat_toolbar_group_title)
    TextView mGroupTitleTextView;

    @BindView(R.id.chat_toolbar_group_members)
    TextView mGroupMembersTextView;

    @BindView(R.id.chat_toolbar_extra_image)
    ImageView mExtraImageView;

    @BindView(R.id.file_preview_progress_bar)
    ProgressBar statusProgressBar;

    @Inject
    FileConfirmationPresenter mFileConfirmationPresenter;

    private Uri mSelectedUri;
    private Group mGroup;
    private String mSelectedString;

    @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);

        EmojIconActions emojiIcon = new EmojIconActions(this, mRelativeLayout, mMessageEmojiconEditText, mEmojiButtonImageView);
        emojiIcon.ShowEmojIcon();

        mGroup = getIntent().getParcelableExtra(Constants.EXTRA_GROUP);

        setToolBar();

        if(getIntent().hasExtra(Constants.TEXT_TO_SHARE)) {
            String textToShare = getIntent().getStringExtra(Constants.TEXT_TO_SHARE);

            Intent intent = new Intent();
            intent.putExtra(Constants.TEXT_TO_SHARE, textToShare);

            setResult(-1, intent);
            finish();
        } else {
            String fileUri = getIntent().getStringExtra(Constants.FILENAME);

            mSelectedUri = Uri.parse(fileUri);

            ViewUtils.setProgressBarColor(statusProgressBar, R.color.colorDefault);

            showLoading();

            GlideApp.with(this)
                    .load(fileUri)
                    .placeholder(R.drawable.document)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            hideLoading();

                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            hideLoading();

                            return false;
                        }
                    })
                    .into(new DrawableImageViewTarget(mFileImageView));
        }
    }

    private void setToolBar() {
        if(mGroup.users.size() > 2) {
            mSingleChatTitleTextView.setVisibility(View.GONE);

            mGroupTitleTextView.setVisibility(View.VISIBLE);

            mGroupMembersTextView.setVisibility(View.VISIBLE);

            mGroupMembersTextView.setText(mGroup.getContactsNames());

            mExtraImageView.setImageResource(R.drawable.vertical_dots);

            mChatImageView.setImageResource(mGroup.isHelpGroup() ? R.drawable.avatar_help_group : R.drawable.avatar_team_group);

            mGroupTitleTextView.setText(mGroup.name);
            mGroupTitleTextView.setTextColor(ContextCompat.getColor(this, mGroup.isHelpGroup() ? R.color.colorHelpGroup : R.color.colorTeamGroup));
        } else {
            mSingleChatTitleTextView.setVisibility(View.VISIBLE);
            mSingleChatTitleTextView.setText(mGroup.name);
            mSingleChatTitleTextView.setTextColor(ContextCompat.getColor(this, mGroup.onlyEmployees ? de.beyondsoft.ownchat.R.color.colorBlack : de.beyondsoft.ownchat.R.color.colorHelpGroup));

            if(TextUtils.isEmpty(mGroup.avatar)) {
                mChatImageView.setImageResource(mGroup.onlyEmployees ? de.beyondsoft.ownchat.R.drawable.avatar : R.drawable.avatar_help);
            } else {
                GlideApp.with(this)
                        .asBitmap()
                        .load(mGroup.avatar)
                        .placeholder(de.beyondsoft.ownchat.R.drawable.avatar)
                        .into(mChatImageView);
            }
        }
    }

    @OnClick(R.id.file_send_message)
    void sendFile() {
        Intent intent = new Intent();

        if(mSelectedUri != null) {
            intent.putExtra(Constants.FILE_PATH, mSelectedUri.toString());
        }

        intent.putExtra(Constants.FILE_MESSAGE_TEXT, mMessageEmojiconEditText.getText().toString());

        setResult(-1, intent);
        finish();
    }

    @OnClick(R.id.chat_toolbar_back_arrow)
    void backPressed() {
        deleteRecording();

        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        deleteRecording();

        super.onBackPressed();
    }

    private void deleteRecording() {
        if(mSelectedUri.toString().contains(Constants.AUDIO_RECORDINGS_DIR)) {
            File audioFile = new File(mSelectedUri.toString());

            if(audioFile.exists()) {
                audioFile.delete();
            }
        }
    }

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

    @Override
    public void showLoading() {
        statusProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        statusProgressBar.setVisibility(View.GONE);
    }

    @Override
    public void showContent() {

    }

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

    }

    @Override
    public void showError(String message) {

    }
}
