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.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bc_chat.bc_base.contract.MucRedPacketContract;
import com.bc_chat.bc_base.entity.wallet.MucRedPacketDetailEntity;
import com.bc_chat.bc_base.presenter.MucRedPacketPresenter;
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.Collections;
import java.util.Date;
import java.util.List;

/**
 * modify by zq
 * 单聊 普通红包、口令红包自己都不可领取
 * 群组 手气红包、口令红包自己可领取，普通红包不可领取
 */
public class MucRedDetailsActivity extends BaseActivity<MucRedPacketPresenter> implements View.OnClickListener, MucRedPacketContract.View {
    LayoutInflater inflater;
    DecimalFormat df = new DecimalFormat("######0.00");
    private CircleImageView red_head_iv;
    private TextView red_nickname_tv;
    private TextView red_words_tv;
    private TextView red_money_tv;
    private TextView red_resultmsg_tv;
    private String bribery_id;
    private ListView red_details_lsv;
    private List<MucRedPacketDetailEntity.GetDetailBean> getDetailBeanList;
    private boolean isOpen;

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

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

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

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

    private void initData() {
        Intent intent = getIntent();
        bribery_id = intent.getStringExtra("bribery_ID");
        if (!TextUtils.isEmpty(bribery_id)) {
            isOpen = intent.getBooleanExtra("is_open", false);
            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);
        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);
        red_details_lsv = (ListView) findViewById(R.id.red_details_lsv);
        inflater = LayoutInflater.from(this);

        initListener();

    }

    @Override
    public void onClick(View view) {

    }

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


    private void showView(MucRedPacketDetailEntity entity) {
        if (entity != null) {
            red_resultmsg_tv.setText("已领取" + entity.getHas_count() + "/" + entity.getNum() + "个,共" + entity.getAmount() + "元");
            GlideUtils.loadCircleImage(this, entity.getSender_avatar(), red_head_iv);
            red_nickname_tv.setText(entity.getSender_name() + "的红包");
            red_money_tv.setText(entity.getMyMoney());
            red_words_tv.setText(entity.getTitle());

            getDetailBeanList = entity.getGet_detail();
            if (getDetailBeanList != null && getDetailBeanList.size() > 0) {
                RedAdapter redAdapter = new RedAdapter();
                redAdapter.preload();
                red_details_lsv.setAdapter(redAdapter);
            }
        }
    }

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

    @Override
    public void redPacketDetailSuccess(@Nullable MucRedPacketDetailEntity mucRedPacketDetailEntity) {
        showView(mucRedPacketDetailEntity);
    }


    private class RedAdapter extends BaseAdapter {
        View view;

        @Override
        public int getCount() {
            return getDetailBeanList == null ? 0 : getDetailBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MucRedPacketDetailEntity.GetDetailBean entity = getDetailBeanList.get(position);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            long lcc_time = Long.valueOf(entity.getTake_time());
            String StrTime = sdf.format(new Date(lcc_time * 1000L));

            view = inflater.inflate(R.layout.reditem_layout, null);
            TextView username_tv = (TextView) view.findViewById(R.id.username_tv);
            username_tv.setText(entity.getReceiver_name());
            TextView money_tv = (TextView) view.findViewById(R.id.money_tv);
            money_tv.setText(entity.getAmount() + "元");
            TextView opentime_tv = (TextView) view.findViewById(R.id.opentime_tv);
            opentime_tv.setText(StrTime);
            CircleImageView iv_avatar = view.findViewById(R.id.iv_avatar);
            GlideUtils.loadCircleImage(MucRedDetailsActivity.this, entity.getReceiver_avatar(), iv_avatar);

            return view;
        }

        public void preload() {
            // 按时间排序，避免手气最佳固定在最上面，
            Collections.sort(getDetailBeanList, (o1, o2) ->
                    // 时间大的排上面，
                    -Integer.compare(Integer.parseInt(o1.getTake_time()), Integer.parseInt(o2.getTake_time()))
            );
        }
    }
}
