package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.graphics.Color;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.di.components.DaggerRechargeQComponent;
import com.sheep.gamegroup.di.modules.RechargeQModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.RechargeQEntity;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.presenter.RechargeQContract;
import com.sheep.gamegroup.presenter.RechargeQPresenter;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.MyDbManager;
import com.sheep.gamegroup.util.MyGridview;
import com.sheep.gamegroup.view.adapter.RechargeQAdapter;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.RECHARGE_QQ_PAY;

/**
 * Q币充值
 */

public class RechargeQAct extends BaseActivity implements RechargeQContract.View {

    @Inject
    RechargeQPresenter presenter;
    @BindView(R.id.tv_zk)
    TextView tvZk;
    @BindView(R.id.rechargeq_greidview)
    MyGridview rechargeqGreidview;
    @BindView(R.id.et_qb_number)
    EditText etQbNumber;
    @BindView(R.id.tv_myb_number)
    TextView tvMybNumber;
    @BindView(R.id.tv_myb_recharge)
    TextView tvMybRecharge;
    @BindView(R.id.et_qq)
    EditText etQq;
    @BindView(R.id.rechargeq_sure)
    TextView rechargeqSure;
    private List<RechargeQEntity> wlLists = new ArrayList<>();
    private RechargeQAdapter adapter;
    private int select = -1;

    String text_qq;//充值QQ号
    String text_QBNumber;//充值金额

    private UserEntity userEntity;

    @Override
    protected int getLayoutId() {
        return R.layout.rechargeq_layout;
    }

    @Override
    public void initView() {
        TitleBarUtils
                .getInstance()
                .setShowOrHide(this, true)
                .setTitle(this, "Q币充值")
                .setTitleFinish(this);
        DaggerRechargeQComponent.builder()
                .netComponent(SheepApp.getInstance().getNetComponent())
                .rechargeQModule(new RechargeQModule(this))
                .build()
                .inject(this);
        initRechargeDatas();
        adapter = new RechargeQAdapter(this, wlLists);
        rechargeqGreidview.setVerticalSpacing(20);
        rechargeqGreidview.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        rechargeqGreidview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                etQbNumber.setText("");
                select = position;
                RechargeQEntity rechargeQEntity = (RechargeQEntity) parent.getItemAtPosition(position);
                for (int i = 0; i < wlLists.size(); i++) {
                    if (position == i) {
                        wlLists.get(i).setSelectState(true);
                    } else {
                        wlLists.get(i).setSelectState(false);
                    }
                }
                adapter.notifyDataSetChanged();
                text_QBNumber = rechargeQEntity.getPrice();
//                tvMybNumber.setText(String.format(getString(R.string.rechargeq_amount),text_QBNumber));
            }
        });

        tvZk.setText(getSpannableString());
        userEntity = DataUtil.getInstance().getUserEntity();
        if(userEntity != null){
            tvMybNumber.setText(String.format(getString(R.string.rechargeq_amount),userEntity.getBalance() +""));
        }else {
            userEntity = DataUtil.getInstance().getUserEntity();
            if(userEntity == null){
                tvMybNumber.setText(String.format(getString(R.string.rechargeq_amount),"0"));
                return;
            }
            DataUtil.getInstance().setUserEntity(userEntity);
            tvMybNumber.setText(String.format(getString(R.string.rechargeq_amount),userEntity.getBalance() +""));

        }


        etQbNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(TextUtils.isEmpty(s)){
                    adapter.setInPut(false);
                    if(select != -1 ){
                        RechargeQEntity rechargeQEntity = adapter.getItem(select);
                        text_QBNumber = rechargeQEntity.getPrice();
//                        tvMybNumber.setText(String.format(getString(R.string.rechargeq_amount),text_QBNumber));
                    }
                }else{
                    adapter.setInPut(true);
//                    tvMybNumber.setText(String.format(getString(R.string.rechargeq_amount),s.toString()));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    /**
     * 初始化写死充值数据
     */
    private void initRechargeDatas(){
        wlLists.add(new RechargeQEntity(false,"10","10Q币"));
        wlLists.add(new RechargeQEntity(false,"20","20Q币"));
        wlLists.add(new RechargeQEntity(false,"50","50Q币"));
        wlLists.add(new RechargeQEntity(false,"100","100Q币"));
        wlLists.add(new RechargeQEntity(false,"200","200Q币"));
        wlLists.add(new RechargeQEntity(false,"500","500Q币"));
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {


    }

    @OnClick({R.id.tv_myb_recharge, R.id.rechargeq_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_myb_recharge:
                Jump2View.getInstance().goRechargeAct(this, "充值Q币");
                break;
            case R.id.rechargeq_sure:
                RECHARGE_QQ_PAY.onEvent();
                if (TextUtils.isEmpty(text_QBNumber)) {
                    text_QBNumber = etQbNumber.getText().toString().trim();
                }
                if (TextUtils.isEmpty(text_QBNumber)) {
                    G.showToast("请选择充值Q币数量");
                    return;
                }
                text_qq = etQq.getText().toString().trim();
                if (TextUtils.isEmpty(text_qq)) {
                    G.showToast("请输入QQ号");
                    return;
                }
                showProgress();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("amount", NumberFormatUtils.parseInteger(text_QBNumber));
                jsonObject.put("qq", text_qq);
                presenter.goRechargeQ(jsonObject);
                break;
        }
    }

    @Override
    public void succRechargeQ(BaseMessage o) {
        hideProgress();
//   "模拟支付宝成功，因为支付宝支持成功返回9000，跳转到统一界面"
        Jump2View.getInstance().goRechargeResultAct(RechargeQAct.this, "9000");
        finish();
    }

    @Override
    public void failRechargeQ(BaseMessage o) {
        hideProgress();
        G.showToast(o.getMsg());
    }


    private SpannableString getSpannableString(){
        SpannableString spannableString = new SpannableString("(使用绵羊币充值享95折优惠)");
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff0006"));
        spannableString.setSpan(colorSpan, 9, 11, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }
}
