package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.absBase.AbsChooseImageActivity;
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.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 新手添加QQ群任务详情页面
 * Created by realicing on 2018/4/26.
 */

public class TaskDetailAddQQAct extends AbsChooseImageActivity {

    @BindView(R.id.detail_item_layout_1)
    View detail_item_layout_1;
    @BindView(R.id.name_tv)
    TextView nameTv;
    @BindView(R.id.date_tv)
    TextView dateTv;
    @BindView(R.id.price_tv)
    TextView priceTv;
    @BindView(R.id.detail_task_tv)
    TextView detailTaskTv;
    @BindView(R.id.web_text)
    WebView webText;
    @BindView(R.id.ll_infos)
    LinearLayout llInfos;
    @BindView(R.id.btn_task_item)
    TextView btnTaskItem;
    @BindView(R.id.btn_up_imag)
    TextView btnUpImag;
    @BindView(R.id.bottom_btn_layout)
    LinearLayout bottom_btn_layout;
    @BindView(R.id.detail_item_layout_2)
    LinearLayout detail_item_layout_2;
    @BindView(R.id.detail_item_layout_3)
    LinearLayout detail_item_layout_3;
    @BindView(R.id.detail_shrae_friend_layout)
    LinearLayout detail_shrae_friend_layout;
    @BindView(R.id.item_layout)
    View item_layout;
    private Activity activity;

    private NewbieTask newbieTask;

    @Override
    protected int getLayoutId() {
        return R.layout.task_detail_layout;
    }

    @Override
    public void initView() {
        activity = this;
        newbieTask = (NewbieTask) getIntent().getSerializableExtra("task_entity");
        TitleBarUtils.getInstance()
                .setTitle(activity, "加入官方QQ群任务详情")
                .setTitleFinish(activity);
        detail_item_layout_1.setVisibility(View.GONE);
        detail_item_layout_3.setVisibility(View.GONE);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        if(newbieTask == null){
            G.showToast("网络异常，请稍候再试");
//            finish();
            return;
        }
        item_layout.setBackgroundResource(R.drawable.sp_bg_white_solid_top);
        detail_item_layout_2.setVisibility(View.VISIBLE);
        detail_item_layout_3.setVisibility(View.VISIBLE);
        detail_shrae_friend_layout.setVisibility(View.GONE);
        webText.setWebChromeClient(new WebChromeClient(){

        });
        webText.getSettings().setJavaScriptEnabled(true);

// 1、LayoutAlgorithm.NARROW_COLUMNS ： 适应内容大小
// 2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕，内容将自动缩放
//        webText.getSettings().setUseWideViewPort(true);
//        webText.getSettings().setLoadWithOverviewMode(true);
//        webText.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//        webText.setInitialScale(80);
        webText.setWebViewClient(new WebViewClient(){
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadDataWithBaseURL(null,request.getUrl().toString(), "text/html", "utf-8", null);
                return true;
            }
        });
        //封装头文件
        String sHead=   "<html><head><meta name=\"viewport\" content=\"width=device-width, " +
                "initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes\" />"+
                "<style>img{max-width:100% !important;height:auto !important;}</style>"
                +"<style>body{max-width:100% !important;}</style>"+"</head><body>";
        webText.loadDataWithBaseURL(null,sHead + newbieTask.getDesc() + "</body></html>", "text/html", "utf-8", null);


        detailTaskTv.setVisibility(View.INVISIBLE);
        bottom_btn_layout.setVisibility(View.VISIBLE);
            btnTaskItem.setVisibility(View.GONE);
    }

    @OnClick({R.id.btn_up_imag})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_up_imag:
                showChooseDialog(false, true, DEFAULT_MAX_COUNT);
                UMConfigUtils.onEvent(UMConfigUtils.Event.TASK_UPLOAD_PICTURE);
                break;
        }
    }

    @Override
    protected void onNotGetImage(String msg) {
        G.showToast(msg);
    }

    @Override
    protected void onGetImage(String path) {

    }

    @Override
    protected void onSetNetImageUrl(final String url) {
        setLoaddingText("更新中");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ext", url);
        if(newbieTask.getUser_task_id() == 0) {//第一次提交
            SheepApp.getInstance().getNetComponent().getApiService().newbieTaskAddQq(jsonObject)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(getApplicationContext()) {
                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            setLoaddingText("完成");
                            onGetNetImageUrl(url);
                            G.showToast("提交成功，等待审核!");
                            dismissLoaddingDialog();
                            finish();
                        }

                        @Override
                        public void onError(BaseMessage baseMessage) {
                            updateError(baseMessage.getErrorMsg());
                        }
                    });
        } else {//第一次提交已经失败，第二次提交接口不一样。。。。。。
            jsonObject.put("id", newbieTask.getUser_task_id());
            SheepApp.getInstance().getNetComponent().getApiService().putNewbieTask(jsonObject)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(getApplicationContext()) {
                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            setLoaddingText("完成");
                            onGetNetImageUrl(url);
                            G.showToast("提交成功，等待审核!");
                            dismissLoaddingDialog();
                            finish();
                        }

                        @Override
                        public void onError(BaseMessage baseMessage) {
                            updateError(TextUtils.isEmpty(baseMessage.getMsg()) ? "服务器错误，请稍候再试" : baseMessage.getMsg());
                        }
                    });
        }
    }

    @Override
    protected void onGetNetImageUrl(String url) {

    }
}
