package de.beyondsoft.ownchat.page.fileconfirmation;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;

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.FileUtils;

import java.io.File;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
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;

    @Inject
    FileConfirmationPresenter mFileConfirmationPresenter;

    private Uri mSelectedUri;
    private Group mGroup;

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

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

        mSelectedUri = Uri.parse(fileUri);

        Glide.with(this)
                .load(getFile(mSelectedUri).getPath())
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(new GlideDrawableImageViewTarget(mFileImageView));

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

    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 {
                Glide.with(this).load(mGroup.avatar)
                        .placeholder(de.beyondsoft.ownchat.R.drawable.avatar)
                        .dontAnimate()
                        .into(mChatImageView);
            }
        }
    }

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

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

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

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

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