package com.sheep.gamegroup.absBase;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.TextView;

import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.greendao.download.DownLoadInfo;
import com.sheep.gamegroup.helper.DownloadHelper;
import com.sheep.gamegroup.util.DownloadTextUtl;
import com.sheep.gamegroup.util.DownloadUtil;
import com.sheep.jiuyan.samllsheep.utils.PackageUtil;

import rx.functions.Action1;

/**
 * Created by realicing on 2018/11/1.
 * realicing@sina.com
 */
public abstract class AbsGetDownloadListener {
    private boolean showSimpleText;

    public AbsGetDownloadListener(boolean showSimpleText) {
        this.showSimpleText = showSimpleText;
    }

    private AbsDownloadListener absDownloadListener = new AbsDownloadListener() {
        @Override
        public void removedApk(final String packageName) {
            checkPackageName(packageName, new ICallBack2<TextView, String>() {
                @Override
                public void call(TextView textView, String downloadUrl) {
                    String path = PackageUtil.isExistsFile(packageName, downloadUrl);
                    if (TextUtils.isEmpty(path)) {
                        textView.setText(DownloadTextUtl.getStartDownloadText(showSimpleText));
                    } else {
                        textView.setText(DownloadTextUtl.getStartInstallText(showSimpleText));
                    }
                }
            });
        }

        @Override
        public void addedApk(String packageName) {
            checkPackageName(packageName, new ICallBack2<TextView, String>() {
                @Override
                public void call(TextView textView, String downloadUrl) {
                    textView.setText(DownloadTextUtl.getStartOpenText(showSimpleText));
                }
            });
        }

        @Override
        public void running(final DownLoadInfo task) {
            checkUrl(task.getMDownloadUrl(), new Action1<TextView>() {
                @Override
                public void call(TextView textView) {
                    if (textView != null) {
                        textView.setText(DownloadTextUtl.getProgressText(showSimpleText, task));
                    }
                }
            });
        }

        @Override
        public void taskStop(final DownLoadInfo task) {
            checkUrl(task.getMDownloadUrl(), new Action1<TextView>() {
                @Override
                public void call(TextView textView) {
                    if (textView != null) {
                        textView.setText(DownloadTextUtl.getContinueDownloadText(showSimpleText));
                    }
                }
            });
        }

        @Override
        public void taskComplete(final DownLoadInfo task) {
            checkUrl(task.getMDownloadUrl(), new Action1<TextView>() {
                @Override
                public void call(TextView textView) {
                    if (textView != null) {
                        textView.setText(DownloadTextUtl.getStartInstallText(showSimpleText));
                        DownloadHelper downloadHelper = getDownloadHelper(task.getMDownloadUrl());
                        if (downloadHelper != null)
                            downloadHelper.updateState(DownloadUtil.STATUS_FINISH);
                    }
                }
            });
        }

        @Override
        public void taskCancel(final DownLoadInfo task) {
            checkUrl(task.getMDownloadUrl(), new Action1<TextView>() {
                @Override
                public void call(TextView textView) {
                    if (textView != null) {
                        textView.setText(DownloadTextUtl.getTaskCancelText(showSimpleText));
                    }
                }
            });
        }

        @Override
        public void taskFail(final DownLoadInfo task) {
            checkUrl(task.getMDownloadUrl(), new Action1<TextView>() {
                @Override
                public void call(TextView textView) {
                    if (textView != null) {
                        textView.setText(DownloadTextUtl.getFailDownloadText(showSimpleText));
                    }
                }
            });
        }
    };


    private void checkUrl(String downloadUrl, Action1<TextView> action1) {
        try {
            action1.call(getTextView(downloadUrl));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void checkPackageName(String packageName, ICallBack2<TextView, String> action1) {
        try {
            action1.call(getTextView2(packageName), getDownloadUrl(packageName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //根据包名获取下载地址
    public abstract String getDownloadUrl(String packageName);

    //根据包名获取TextView
    public abstract TextView getTextView2(String packageName);

    //根据下载地址获取TextView
    public abstract TextView getTextView(String downloadUrl);

    //根据下载地址获取DownloadHelper
    public abstract DownloadHelper getDownloadHelper(String downloadUrl);


    public void onEventMainThread(Intent intent) {
        absDownloadListener.onEventMainThread(intent);
    }

    public void onEventMainThread(BigEvent bigEvent) {
        absDownloadListener.onEventMainThread(bigEvent);
    }
}
