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

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
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.DatePicker;
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 java.util.Calendar;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 通知界面
 * @date 2019/10/28
 */
public class NoticeFragment extends BaseFragment {

    private String mServerPath, mLocalPath;
    private String mDate;
    private int mType;
    private static final String DATE_FORMAT = "%d%02d%02d";
    private int mYear, mMonth, mDay;

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

    @OnClick({R.id.tv_query_date, R.id.btn_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_query_date:
                new DatePickerDialog(activity, onPickDateListener, mYear, mMonth, mDay).show();
                break;
            case R.id.btn_confirm:
                mDate = tvDate.getText().toString().trim();
                mServerPath = TCPDownload.SERVER_HISTORY_NOTICE + mDate + ".txt";
                mLocalPath = SystemNI.getDownloadPath() + "/notice" + mDate;
                String[] paths = new String[]{mServerPath, mLocalPath};
                DownLoadNoticeTask task = new DownLoadNoticeTask();
                task.execute(paths);
                break;
        }
    }

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

    @Override
    public void onCreateViewFinished(Bundle state) {
        if(getArguments() != null){
            mType = getArguments().getInt(Constant.KEY_TYPE);
        }
        tvNotice.setMovementMethod(ScrollingMovementMethod.getInstance());
        if (mType == InfoActivity.TYPE_HISTORY) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(System.currentTimeMillis());
            mYear = cal.get(Calendar.YEAR);
            mMonth = cal.get(Calendar.MONTH);
            mDay = cal.get(Calendar.DAY_OF_MONTH);
            tvDate.setText(String.format(Locale.getDefault(), DATE_FORMAT, mYear, mMonth + 1, mDay));
            layoutDate.setVisibility(View.VISIBLE);
        } else {
            mServerPath = TCPDownload.SERVER_NOTICE;
            mLocalPath = SystemNI.getDownloadPath() + TCPDownload.LOCAL_NOTICE;
            String[] paths = new String[]{mServerPath, mLocalPath};
            DownLoadNoticeTask task = new DownLoadNoticeTask();
            task.execute(paths);
        }
    }

    private DatePickerDialog.OnDateSetListener onPickDateListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            tvDate.setText(String.format(Locale.getDefault(), DATE_FORMAT, mYear, mMonth + 1, mDay));
        }
    };

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

    @SuppressLint("StaticFieldLeak")
    class DownLoadNoticeTask 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 {
                    layoutDate.setVisibility(View.GONE);
                }
            }
            tvNotice.setText(result);
        }
    }

    @BindView(R.id.tv_notice)
    TextView tvNotice;
    @BindView(R.id.tv_query_date)
    TextView tvDate;
    @BindView(R.id.layout_date_select)
    ViewGroup layoutDate;
}
