package com.genlot.ninglottery.module.info.wininfo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.genlot.nativesdk.SystemNI;
import com.genlot.nativesdk.model.WinInfo;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseMVPFragment;
import com.genlot.ninglottery.base.InjectPresenter;
import com.genlot.ninglottery.game.GameManager;
import com.genlot.ninglottery.model.Constant;
import com.genlot.ninglottery.module.info.InfoActivity;

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

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 中奖信息界面
 * @date 2019/10/28
 */
public class WinInfoFragment extends BaseMVPFragment implements IWinInfoView{

    private int mType, mGameId;
    private long mIssue;
    private List<WinInfo> mList;
    private WinInfoAdapter mAdapter;

    @InjectPresenter
    WinInfoPresenter mPresenter;

    public static WinInfoFragment getInstance(int type,int game) {
        Bundle args = new Bundle();
        args.putInt(Constant.KEY_TYPE, type);
        args.putInt(Constant.KEY_GAME, game);
        WinInfoFragment fragment = new WinInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @OnClick({R.id.btn_confirm})
    public void onClick(View view) {
        String issue = etIssue.getText().toString().trim();
        if (TextUtils.isEmpty(issue) || issue.length() < 7) {
            etIssue.setError("期号输入不正确!");
            return;
        }
        mIssue = Long.parseLong(issue);
        tvIssue.setText(String.valueOf(mIssue));
        mPresenter.queryWinInfo(mGameId, mIssue);
    }

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

    @Override
    public void onCreateViewFinished(Bundle state) {
        super.onCreateViewFinished(state);
        if(getArguments() != null){
            mType = getArguments().getInt(Constant.KEY_TYPE);
            mGameId = getArguments().getInt(Constant.KEY_GAME);
        }
        mList = new ArrayList<>();
        mAdapter = new WinInfoAdapter(R.layout.item_win_info, mList);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setAdapter(mAdapter);
        tvStation.setText(String.valueOf(SystemNI.getTerminalID()));
        if (mType == InfoActivity.TYPE_HISTORY) {
            layoutIssue.setVisibility(View.VISIBLE);
            llContent.setVisibility(View.GONE);
        } else {
            mIssue = Objects.requireNonNull(GameManager.getGameRuntime(mGameId)).mGameIssue - 1;
            tvIssue.setText(String.valueOf(mIssue));
            mPresenter.queryWinInfo(mGameId, mIssue);
        }
    }

    public boolean onBack() {
        if (mType == InfoActivity.TYPE_HISTORY) {
            if (layoutIssue.getVisibility() == View.GONE) {
                layoutIssue.setVisibility(View.VISIBLE);
                llContent.setVisibility(View.GONE);
                return true;
            }
        }
        return false;
    }

    @Override
    public void onQuerySuccess(ArrayList<WinInfo> list) {
        activity.runOnUiThread(() -> {
            if (mType == InfoActivity.TYPE_HISTORY) {
                layoutIssue.setVisibility(View.GONE);
                llContent.setVisibility(View.VISIBLE);
            }
            if (list != null && list.size() > 0) {
                layoutWinInfo.setVisibility(View.VISIBLE);
                tvNoPrize.setVisibility(View.GONE);
                mAdapter.replaceData(list);
            } else {
                layoutWinInfo.setVisibility(View.GONE);
                tvNoPrize.setVisibility(View.VISIBLE);
            }
        });
    }

    @BindView(R.id.layout_issue_select)
    ViewGroup layoutIssue;
    @BindView(R.id.et_query_issue)
    EditText etIssue;

    @BindView(R.id.tv_station)
    TextView tvStation;
    @BindView(R.id.tv_issue)
    TextView tvIssue;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.layout_win_info)
    LinearLayout layoutWinInfo;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_no_prize)
    TextView tvNoPrize;

    class WinInfoAdapter extends BaseQuickAdapter<WinInfo, BaseViewHolder>{

        WinInfoAdapter(int layoutResId, @Nullable List<WinInfo> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, WinInfo item) {
            helper.setText(R.id.tv_left, item.winLevelName);
            helper.setText(R.id.tv_mid, item.winCount);
            helper.setText(R.id.tv_right, item.winAmount);
        }
    }

}
