package com.example.newframtool.view;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.ScaleAnimation;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.newframtool.R;
import com.example.newframtool.helper.EventHelper;
import com.example.newframtool.model.FinishListenr;
import com.example.newframtool.model.MyLinearLayout;
import com.example.newframtool.util.LogUtil;

import static android.webkit.WebSettings.LOAD_DEFAULT;

/**
 * Created by dufangyu on 2017/6/16.
 */

public class CurworkView extends ViewImpl {


    private WebView webView;
    private ImageView backImg;
    private TextView title;
    private TextView mushuTv,suduTv,shichangTv,dizhiTv,jijuTv,kuanfuTv;
    private RelativeLayout re_load;
    private ImageView shuaxinImg,locateImg;
    private Animation animation;
    private LinearLayout imglayout;
    private MyLinearLayout datalayout;
    private Handler myHandler =  new Handler();
    private TextView reportTv;

    private String oldWorkAreaValue;


    private LinearLayout lastLayout;
    private TextView lastworkAreaTv,lastworkTimeTv;

    @Override
    public void initView() {
        backImg = findViewById(R.id.backImage);
        backImg.setVisibility(View.VISIBLE);

        title = findViewById(R.id.titletext);
        reportTv = findViewById(R.id.reporttimetv);
        re_load = findViewById(R.id.re_loadPro);
        mushuTv = findViewById(R.id.mushutv);
        suduTv = findViewById(R.id.sudutv);
        shichangTv = findViewById(R.id.shichangtv);
        kuanfuTv = findViewById(R.id.zuoyekuanfutv);
        jijuTv = findViewById(R.id.zuoyejijutv);
        dizhiTv = findViewById(R.id.dizhitv);
        shuaxinImg = findViewById(R.id.shuaxinImg);
        locateImg = findViewById(R.id.locatImg);

        imglayout = findViewById(R.id.imglayout);
        datalayout = findViewById(R.id.datalayout);
        webView = findViewById(R.id.webview);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);// 这个很关键
        settings.setLoadWithOverviewMode(true);
        settings.setCacheMode(LOAD_DEFAULT);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
//                LogUtil.d("dfy","地图 onPageStarted");
                re_load.setVisibility(View.VISIBLE);
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                @SuppressWarnings("ResourceType")
                Animator animator = AnimatorInflater.loadAnimator(mRootView.getContext(), R.animator.anim_file);
                animator.setTarget(datalayout);
                animator.setInterpolator(new BounceInterpolator());
                animator.start();
                datalayout.setVisibility(View.VISIBLE);


                re_load.setVisibility(View.GONE);
                //通知Activity地图加载完成，开启定时器
                ((FinishListenr)mPresent).onFinished();

                myHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        showImageAnim(imglayout);
                        showImageAnim(lastLayout);
                    }
                },800);
            }

            @Override
            public void onPageCommitVisible(WebView view, String url) {
                super.onPageCommitVisible(view, url);

            }
        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {

                LogUtil.d("dfy","message = "+message);
                //防止打印alert信息，页面卡死一定要cancle
                result.cancel();
                return true;
            }
        });
        webView.addJavascriptInterface(new JsConnect(),"webkit");
//        webView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                return (event.getAction()==MotionEvent.ACTION_MOVE);
//            }
//        });



        lastLayout = findViewById(R.id.last_layout);
        lastworkAreaTv = findViewById(R.id.lastworkareatv);
        lastworkTimeTv = findViewById(R.id.lastworktimetv);





    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_cur_work;
    }

    @Override
    public void bindEvent() {
        EventHelper.click(mPresent,backImg,locateImg,shuaxinImg);
        datalayout.setFinishListenr((FinishListenr) mPresent);
    }



    //销毁地图
    public void recycle()
    {
        if(webView!=null)
        {
            webView.destroy();
            webView = null;
        }
    }






    public void setMapUrl(String url)
    {
        webView.loadUrl(url);
    }


    public void setTitleText(String titleText)
    {
        title.setText(titleText);
    }


    public void hideLoad()
    {
        re_load.setVisibility(View.GONE);
    }



    //点击刷新按钮
    public void callJsRefresh()
    {
        webView.loadUrl("javascript:FollowFun('"+1+"')");
    }





    public void startRefresh()
    {
        //此方法是用xml文件方式来实现动画效果
        animation = AnimationUtils.loadAnimation(mRootView.getContext(), R.anim.spinner);
        //动画完成后，是否保留动画最后的状态，设为true
        animation.setFillAfter(true);
        if(animation!=null)
        {
            shuaxinImg.startAnimation(animation);
        }
        shuaxinImg.setEnabled(false);

    }


    public void cancleAnim()
    {
        if(animation!=null)
        {
            myHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    shuaxinImg.clearAnimation();
                    shuaxinImg.setVisibility(View.INVISIBLE);
                    animation = null;
                }
            },500);
        }

    }

    //是否开启刷新按钮显示定时器
    public boolean isBeginTimer()
    {
        return animation!=null;
    }


    public void setRefreshImgEnable()
    {
        shuaxinImg.setEnabled(true);
        shuaxinImg.setVisibility(View.VISIBLE);
    }
    //点击定位按钮
    public void callJsLocate()
    {
        webView.loadUrl("javascript:Relocate()");
    }


    public String getWorkAreaValue()
    {
        return oldWorkAreaValue;
    }


    public void setData(String mushuStr,String suduStr,String shichangStr,String kuanfuStr,String jijuStr,String dizhiStr,String time)
    {
//        hideLoad();
        if(TextUtils.isEmpty(mushuStr))
        {
            mushuTv.setText("未知");
        }else{
            mushuTv.setText(mushuStr);
            oldWorkAreaValue = mushuStr;
        }
        //---------------------------
        if(TextUtils.isEmpty(suduStr))
        {
            suduTv.setText("未知");
        }else{
            suduTv.setText(suduStr);
        }
        //---------------------------
        if(TextUtils.isEmpty(shichangStr))
        {
            shichangTv.setText("0");
        }else{
            shichangTv.setText(shichangStr);
        }
        //---------------------------
        if(TextUtils.isEmpty(kuanfuStr))
        {
            kuanfuTv.setText("未知");
        }else{
            kuanfuTv.setText(kuanfuStr+"m");
        }
        //---------------------------
        if(TextUtils.isEmpty(jijuStr))
        {
            jijuTv.setText("未知");
        }else{
            jijuTv.setText(jijuStr);
        }
        //---------------------------
        if(TextUtils.isEmpty(dizhiStr))
        {
            dizhiTv.setText("未知");
        }else{
            dizhiTv.setText(dizhiStr);
        }



        if(TextUtils.isEmpty(time))
        {
            reportTv.setText(" ");
        }else{
            reportTv.setText(time);
        }


    }




    public void setLastWorkData(String workArea,String workTime)
    {
        if(TextUtils.isEmpty(workArea))
        {
            lastworkAreaTv.setText("0.00亩");
        }else{
            lastworkAreaTv.setText(workArea+"亩");
        }

        if(TextUtils.isEmpty(workTime))
        {
            lastworkTimeTv.setText("0分");
        }else{
            lastworkTimeTv.setText(workTime+"分");
        }

    }



    public class JsConnect {

        @JavascriptInterface
        public void javaMethod(final String response){
//            LogUtil.d("dfy","后台返回数据 response = "+response);
            myHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if(!TextUtils.isEmpty(response))
                                mPresent.callBackActivity(response,"","","","");
                        }
                    },0);
        }

    }



    private void showImageAnim(LinearLayout layout)
    {

        if(layout.getId()==R.id.last_layout)
        {
            if(layout.getVisibility()==View.INVISIBLE)
            {
                lastLayout.startAnimation(AnimationUtils.loadAnimation(mRootView.getContext(), R.anim.rili_anim_from));
                layout.setVisibility(View.VISIBLE);
            }

        }else{
            for(int i =0;i<layout.getChildCount();i++)
            {
                final View child = layout.getChildAt(i);

                myHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ScaleAnimation aa = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                        aa.setDuration(500);
                        child.startAnimation(aa);
                        child.setVisibility(View.VISIBLE);
                    }
                },i*500);

            }
        }

    }


}
