package com.sheep.gamegroup.view.dialog;

import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.NewbieTask;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.gamegroup.view.adapter.NewbieTaskAdp;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


public class DialogNewbieTaskList {

    public static AlertDialog showDialog(final Activity activity, List<NewbieTask> newbieTaskList) {
        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_NEWBIE_TASK_LIST_DIALOG);

        View dialog_parent = View.inflate(activity, R.layout.dialog_parent, null);
        final AlertDialog dialog = new AlertDialog.Builder(activity, R.style.MyDialogActivityTheme)
                .setView(dialog_parent)
                .create();
        TextView dialog_title = dialog_parent.findViewById(R.id.dialog_title);
        View dialog_close = dialog_parent.findViewById(R.id.dialog_close);
        LinearLayout dialog_center_ll = dialog_parent.findViewById(R.id.dialog_center_ll);
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_newbie_task_list, dialog_center_ll, true);
        dialog_title.setText("新手任务");


        View.OnClickListener onClickListener =  new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        };
        final ListView dialog_center_lv = view.findViewById(R.id.dialog_center_lv);
        dialog_center_lv.setAdapter(new NewbieTaskAdp(activity, newbieTaskList).setOnClickListener(onClickListener));
        dialog_close.setOnClickListener(onClickListener);
        try {
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dialog;
    }

    public static void tryShowDialog(final Activity activity) {
        if(activity instanceof BaseActivity){
            ((BaseActivity) activity).showProgress();
        }
        SheepApp.getInstance().getNetComponent().getApiService().getNewbieTask()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(activity) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        if(activity instanceof BaseActivity){
                            ((BaseActivity) activity).hideProgress();
                        }
                        List<NewbieTask> newbieTaskList = baseMessage.getDatas(NewbieTask.class);
                        DialogNewbieTaskList.showDialog(activity, newbieTaskList);
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        if(activity instanceof BaseActivity){
                            ((BaseActivity) activity).hideProgress();
                        }
                        G.showToast(baseMessage);
                    }
                });
    }
}
