package com.bc_chat.im.activity.envelopes;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bc_chat.bc_base.contract.ChatRedPacketContract;
import com.bc_chat.bc_base.entity.wallet.ChatRedPacketDetailEntity;
import com.bc_chat.bc_base.presenter.ChatRedPacketPresenter;
import com.bc_chat.im.R;
import com.zhaohaoting.framework.abs.BaseActivity;
import com.zhaohaoting.framework.utils.glid.GlideUtils;
import com.zhaohaoting.framework.view.CircleImageView;

import org.jetbrains.annotations.Nullable;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * modify by zq
 * 单聊 普通红包、口令红包自己都不可领取
 * 群组 手气红包、口令红包自己可领取，普通红包不可领取
 */
public class ChatRedDetailsActivity extends BaseActivity<ChatRedPacketPresenter> implements View.OnClickListener, ChatRedPacketContract.View {
    LayoutInflater inflater;
    DecimalFormat df = new DecimalFormat("######0.00");
    private CircleImageView red_head_iv;
    private CircleImageView civOpenAvatar;
    private TextView red_nickname_tv;
    private TextView red_words_tv;
    private TextView red_money_tv;
    private TextView red_resultmsg_tv;
    private TextView username_tv;
    private TextView money_tv;
    private TextView opentime_tv;
    private LinearLayout llReceiver;
    private String bribery_id;
    private boolean isOpen;

    public static void start(Context context, String bribery_ID, boolean isOpen) {
        Intent intent = new Intent(context, ChatRedDetailsActivity.class);
        intent.putExtra("bribery_ID", bribery_ID);
        intent.putExtra("is_open", isOpen);
        context.startActivity(intent);
    }

    @Override
    protected ChatRedPacketPresenter initPresenter() {
        return new ChatRedPacketPresenter<>(this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_chat_redpacket_details;
    }

    @Override
    protected void initialize() {
        initView();
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        bribery_id = intent.getStringExtra("bribery_ID");
        isOpen = intent.getBooleanExtra("is_open", false);
        if (!TextUtils.isEmpty(bribery_id)) {
            if (isOpen) {
                //已经打开过红包了，直接显示详情
                getPresenter().redPacketDetail(bribery_id);
            } else {
                //先打开红包，再显示详情
                getPresenter().openRedPacket(bribery_id);
            }
        }
    }

    private void initListener() {

    }

    private void initView() {
        red_head_iv = (CircleImageView) findViewById(R.id.red_head_iv);
        civOpenAvatar = (CircleImageView) findViewById(R.id.civOpenAvatar);
        red_nickname_tv = (TextView) findViewById(R.id.red_nickname_tv);
        red_words_tv = (TextView) findViewById(R.id.red_words_tv);
        red_money_tv = (TextView) findViewById(R.id.get_money_tv);
        red_resultmsg_tv = (TextView) findViewById(R.id.red_resultmsg_tv);
        username_tv = (TextView) findViewById(R.id.username_tv);
        money_tv = (TextView) findViewById(R.id.money_tv);
        opentime_tv = (TextView) findViewById(R.id.opentime_tv);
        llReceiver = (LinearLayout) findViewById(R.id.llReceiver);

        initListener();

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void openRedPacketFailure(@Nullable Exception exception) {
        finish();
    }

    @Override
    public void redPacketDetailSuccess(@Nullable ChatRedPacketDetailEntity redPacketDetailEntity) {
        showView(redPacketDetailEntity);
    }

    private void showView(ChatRedPacketDetailEntity redPacketDetailEntity) {
        if (redPacketDetailEntity != null) {

            GlideUtils.loadCircleImage(this, redPacketDetailEntity.getSender_avatar(), red_head_iv);
            red_nickname_tv.setText(redPacketDetailEntity.getSender_name() + "的红包");
            red_money_tv.setText(redPacketDetailEntity.getAmount());
            red_words_tv.setText(redPacketDetailEntity.getTitle());
            int status = Integer.parseInt(redPacketDetailEntity.getStatus());
            if (status == 2) {
                //待领取
                red_resultmsg_tv.setVisibility(View.VISIBLE);
                red_resultmsg_tv.setText("已领取1/1个,共" + redPacketDetailEntity.getAmount() + "元, 已领完！");
                llReceiver.setVisibility(View.VISIBLE);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                long lcc_time = Long.valueOf(redPacketDetailEntity.getTake_time());
                String StrTime = sdf.format(new Date(lcc_time * 1000L));
                opentime_tv.setText(StrTime);
                GlideUtils.loadCircleImage(this, redPacketDetailEntity.getReceiver_avatar(), civOpenAvatar);
                username_tv.setText(redPacketDetailEntity.getReceiver_name());
                money_tv.setText(redPacketDetailEntity.getAmount() + "元");
            }
        }
    }

    @Override
    public void redPacketDetailFailure(@Nullable Exception exception) {
        finish();
    }

}
