package com.sipai.jspatrol.Main.fragment.fifthFragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sipai.jspatrol.Base.ActivityCollector;
import com.sipai.jspatrol.Base.BaseFragmentView;
import com.sipai.jspatrol.Login.LoginActivity;
import com.sipai.jspatrol.NFC.NFCGuideActivity;
import com.sipai.jspatrol.Patrol.ProPatrolTaskListActivity;
import com.sipai.jspatrol.R;
import com.sipai.jspatrol.Service.IPatrolService;
import com.sipai.jspatrol.URL.GuideMenuUrl;
import com.sipai.jspatrol.Webv.GuideMenuActivity;
import com.sipai.jspatrol.Webv.GuideMenuLandscapeActivity;
import com.sipai.jspatrol.background.locationservice.LocationService;
import com.sipai.jspatrol.utils.SharedPreferencesData;

import java.util.HashMap;

import cn.pedant.SweetAlert.SweetAlertDialog;

import static android.content.Context.MODE_PRIVATE;

/**
 * created by lvliming on 2019/9/16
 */
public class FifthFragment extends BaseFragmentView<FifthFragmentPresenter, FifthFragmentContract.View> implements View.OnClickListener {
    private SharedPreferencesData sharedpre;
    private HashMap<String, String> map_base = new HashMap<>();
    private String mServer;
    private String ip_in_plant = "", ip_off_site = "";//厂内，厂外ip地址
    private String mCaption;
    private String mBizid;
    private String mTotaltime;
    private String mLastlogintime;
    private String nfc;
    private RelativeLayout rl_screen;
    private TextView tv_name,tv_totaltime,tv_lastlogin;
    private LinearLayout ll_about,ll_switch,ll_exit,ll_video;
    private Button btn_sub,btn_catch;
    private TextView tv_shift_record;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fifth,container,false);
        init();
        initView(view);
        return view;
    }

    private void initView(View view) {
        tv_name = view.findViewById(R.id.moudle_fifth_fg_tv_name);
        showText(tv_name,mCaption);
        tv_totaltime = view.findViewById(R.id.moudle_fifth_fg_tv_totaltime);
        showText(tv_totaltime,"登录时长：  "+mTotaltime+"  小时");
        tv_lastlogin = view.findViewById(R.id.moudle_fifth_fg_tv_lastlogin);
        showText(tv_lastlogin,"上次登录：  "+mLastlogintime);
        ll_about = view.findViewById(R.id.moudle_fifth_fg_ll_about);
        ll_about.setOnClickListener(this);
        ll_switch = view.findViewById(R.id.moudle_fifth_fg_ll_switch);
        ll_switch.setOnClickListener(this);
        ll_exit = view.findViewById(R.id.moudle_fifth_fg_ll_exit);
        ll_exit.setOnClickListener(this);
        ll_video = view.findViewById(R.id.moudle_fifth_fg_ll_nfc);
        ll_video.setOnClickListener(this);
        if (!nfc.equals("true")){
            ll_video.setVisibility(View.GONE);
        }
        btn_sub = view.findViewById(R.id.moudle_fifth_fg_btn_sub);
        btn_sub.setOnClickListener(this);
        btn_catch = view.findViewById(R.id.moudle_fifth_fg_btn_catch);
        btn_catch.setOnClickListener(this);
        tv_shift_record = view.findViewById(R.id.moudle_fifth_fg_tv_shift_record);
        tv_shift_record.setOnClickListener(this);
    }

    private void showText(final TextView tv, String st) {
        if (tv!=null&&st!=null){
            if (st.isEmpty()){
                st = "";
            }
             if (!getActivity().isFinishing()){
                 final String finalSt = st;
                 getActivity().runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         tv.setText(finalSt);
                     }
                 });
             }
        }
    }

    private void init() {
        sharedpre = new SharedPreferencesData(getActivity());
        map_base = sharedpre.getAll();
        ip_in_plant = map_base.get("ip_in_plant");
        ip_off_site = map_base.get("ip_off_site");
        mServer = map_base.get("mServer");
        mCaption = map_base.get("mCaption");
        mTotaltime = map_base.get("mTotaltime");
        mLastlogintime = map_base.get("mLastlogintime");
        SharedPreferences sharedPre=getActivity().getSharedPreferences("config", MODE_PRIVATE);
        mBizid = sharedPre.getString("bizid", "");
        nfc=sharedPre.getString("nfc", "");
    }

    @Override
    public FifthFragmentContract.View getContract() {
        return null;
    }

    @Override
    public FifthFragmentPresenter getPresenter() {
        return new FifthFragmentPresenter();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.moudle_fifth_fg_tv_shift_record:
                GuideMenuLandscapeActivity.actionStart(getActivity(), GuideMenuUrl.url_shift_record+"?unitId="+mBizid, GuideMenuUrl.name_shift_record);
                break;
            case R.id.moudle_fifth_fg_btn_sub:
                GuideMenuActivity.actionStart(getActivity(), GuideMenuUrl.url_sub+"?unitId="+mBizid, GuideMenuUrl.name_sub);
                break;
            case R.id.moudle_fifth_fg_btn_catch:
                GuideMenuActivity.actionStart(getActivity(), GuideMenuUrl.url_catch+"?unitId="+mBizid, GuideMenuUrl.name_catch);
                break;
            case R.id.moudle_fifth_fg_ll_about:
//                Intent intent = new Intent(getActivity(), AboutOfSoftwareActivity.class);
//                startActivity(intent);
                break;
            case R.id.moudle_fifth_fg_ll_switch:
                showDialog("switch");
                break;
            case R.id.moudle_fifth_fg_ll_exit:
                showDialog("exit");
                break;
            case R.id.moudle_fifth_fg_ll_nfc:
                if (nfc.equals("true")){
                    Intent intent = new Intent(getActivity(), NFCGuideActivity.class);
                    startActivity(intent);
                }else {
                    showToast("请联系管理员开放此权限");
                }

//                Intent intent_video = new Intent(getActivity(), CameraListActivity.class);
//                startActivity(intent_video);
//                Intent intent_video = new Intent(getActivity(), HKWSActivity.class);
//                startActivity(intent_video);
                break;
        }
    }

    private void showToast(String s) {
        if (!getActivity().isFinishing()){
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
                }
            });
        }
    }

    private void showDialog(String st) {
        if (!getActivity().isFinishing()){
            if (st.equals("switch")){
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        new SweetAlertDialog(getActivity(), SweetAlertDialog.WARNING_TYPE)
                                .setTitleText("提示")
                                .setContentText("是否确认切换用户？")
                                .setConfirmText("切换")
                                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        sweetAlertDialog.dismissWithAnimation();
                                        sharedpre.removePwd();
                                        getActivity().finish();
                                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                                        getActivity().startActivity(intent);
                                    }
                                }).setCancelText("取消").show();
                    }
                });
            }else if (st.equals("exit")){
                new SweetAlertDialog(getActivity(), SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("提示")
                        .setContentText("是否确认退出？")
                        .setConfirmText("退出")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                Intent intent = new Intent(getActivity(), IPatrolService.class);
                                getActivity().stopService(intent);
                                Intent intent_loc = new Intent(getActivity(), LocationService.class);
                                getActivity().stopService(intent_loc);
                                sweetAlertDialog.dismissWithAnimation();
                                ActivityCollector.finishAll();
                            }
                        }).setCancelText("取消").show();
            }
        }
    }
}
