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

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.genlot.nativesdk.SystemNI;
import com.genlot.nativesdk.sender.TCPDownload;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseFragment;
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/28
 */
public class BulletinFragment extends BaseFragment {

    private int mType, mGameId;
    private String mServerPath, mLocalPath;

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

    @OnClick({R.id.btn_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                String issue = etIssue.getText().toString().trim();
                if (TextUtils.isEmpty(issue) || issue.length() < 7) {
                    etIssue.setError("期号输入不正确!");
                    return;
                }
                mServerPath = TCPDownload.SERVER_HISTORY_BULLETIN + mGameId + "." + issue + ".txt";
                mLocalPath = SystemNI.getDownloadPath() + "/winbulletin00" + mGameId + "." + issue;
                String[] paths = new String[]{mServerPath, mLocalPath};
                DownLoadTask task = new DownLoadTask();
                task.execute(paths);
                break;
        }
    }

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

    @Override
    public void onCreateViewFinished(Bundle state) {
        if(getArguments() != null){
            mType = getArguments().getInt(Constant.KEY_TYPE);
            mGameId = getArguments().getInt(Constant.KEY_GAME);
        }
        tvBulletin.setMovementMethod(ScrollingMovementMethod.getInstance());
        if (mType == InfoActivity.TYPE_HISTORY) {
            layoutIssue.setVisibility(View.VISIBLE);
        } else {
            mServerPath = TCPDownload.SERVER_BULLETIN + mGameId;
            mLocalPath = SystemNI.getDownloadPath() + "/winbulletin00" + mGameId;
            String[] paths = new String[]{mServerPath, mLocalPath};
            DownLoadTask task = new DownLoadTask();
            task.execute(paths);
        }
    }

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

    @SuppressLint("StaticFieldLeak")
    class DownLoadTask extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            showLoading();
        }

        @Override
        protected String doInBackground(String... voids) {
            mServerPath = voids[0];
            mLocalPath = voids[1];
            int result = TCPDownload.downloadFile(mServerPath, mLocalPath);
            if (result == 0) {
                return TCPDownload.readDownloadFile(mLocalPath);
            }
            return "";
        }

        @Override
        protected void onPostExecute(String result) {
            dismissLoading();
            if (mType == InfoActivity.TYPE_HISTORY) {
                if (TextUtils.isEmpty(result)) {
                    ToastUtils.showShort("当日公告下载失败!");
                } else {
                    layoutIssue.setVisibility(View.GONE);
                }
            }
            tvBulletin.setText(result);
        }
    }

    @BindView(R.id.layout_issue_select)
    ViewGroup layoutIssue;
    @BindView(R.id.tv_bulletin)
    TextView tvBulletin;
    @BindView(R.id.et_query_issue)
    EditText etIssue;
}
