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

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.genlot.nativesdk.sender.TCPDownload;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseFragment;
import com.genlot.ninglottery.game.GameManager;
import com.genlot.ninglottery.model.Constant;
import com.genlot.ninglottery.module.info.InfoActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 站地址
 * @date 2019/10/30
 */
public class AddressFragment extends BaseFragment {

    private int mType;

    @OnClick(R.id.bt_confirm)
    public void onClick() {
        showLoading("下载中...");
        TCPDownload.downloadStationAddress((result, fileName) -> {
            if (result == 0) {
                String address = TCPDownload.readDownloadFile(fileName);
                GameManager.setStationAddress(address);
                dismissLoading();
                ToastUtils.showShort("下载成功");
            } else {
                dismissLoading();
                ToastUtils.showShort("下载失败");
            }
        });
    }

    public static AddressFragment getInstance(int type) {
        Bundle args = new Bundle();
        args.putInt(Constant.KEY_TYPE, type);
        AddressFragment fragment = new AddressFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public int getLayoutResId() {
        return R.layout.fragment_address;
    }

    @Override
    public void onCreateViewFinished(Bundle state) {
        if(getArguments() != null){
            mType = getArguments().getInt(Constant.KEY_TYPE);
        }
        if (mType == InfoActivity.TYPE_NORMAL) {
            tvAddress.setVisibility(View.VISIBLE);
            tvText.setVisibility(View.GONE);
            btConfirm.setVisibility(View.GONE);
            tvAddress.setText(GameManager.getStationAddress());
        } else {
            tvAddress.setVisibility(View.GONE);
            tvText.setVisibility(View.VISIBLE);
            btConfirm.setVisibility(View.VISIBLE);
        }
    }

    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.text1)
    TextView tvText;
    @BindView(R.id.bt_confirm)
    Button btConfirm;
}
