package com.jianrmod.plugins.harmony.Activitys;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.jianrmod.plugins.harmony.Tools.ClientUttls;
import com.jianrmod.plugins.harmony.Tools.HttpInterface;
import com.jianrmod.plugins.harmony.Tools.HttpTools;
import com.jianrmod.plugins.harmony.Tools.Utils;
import com.jianrmod.plugins.harmony.Tools.ZipUtils;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.yutou.jianr_mg.Interfaces.ModInterface;

import java.io.File;

import Interfaces.BaseActivityInterface;


public class MainActivity implements BaseActivityInterface {


    private Context context;
    private KProgressHUD hud;
    private String httpData;

    public MainActivity() {
    }


    @Override
    public void onCreate(Bundle savedInstanceState, LinearLayout layout, final Context context, Intent intent) {
        layout.setOrientation(LinearLayout.VERTICAL);
        this.context = context;
        Utils.handler = new Handler(Looper.getMainLooper());
        hud = Utils.showLoading(context, null, "解压中...");
        layout.addView(getView());
    }


    @Override
    public void onRestart() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void getIntent(Intent intent) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    @Override
    public Data getData() {
        return null;
    }

    private ViewGroup getView() {
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        TextView text1 = new TextView(context);
        Button button = new Button(context);
        Button button2 = new Button(context);
        Button button3=new Button(context);
        final Switch siwtch = new Switch(context);
        siwtch.setText("共存版");
        text1.setText("更新说明(v3)：" +
                "\n1、已支持覆盖版" +
                "\n2、已安装压缩包时下次点击不再解压(如需覆盖解压请删除后缀.unzip)" +
                "\n使用说明：请将proj.manifest、Cocos2dxPrefsFile.xml和压缩包放到jianRMG/harmony/文件夹中" +
                "\n注意，切换客户端版本会影响悬浮窗监听的客户端版本！" +
                "\n注意2！切换客户端版本如果提示“XXXX处理失败，请重启盒子再尝试");
        button.setText("创建harmony文件夹");
        button2.setText("开始");
        button3.setText("不会用？点这里");
        if(Utils.readClientVersionIsbigzhao(context)){
            siwtch.setChecked(false);
            siwtch.setText("共存版");
        }else{
            siwtch.setChecked(true);
            siwtch.setText("覆盖版");
        }

        siwtch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    siwtch.setText("覆盖版");
                    Utils.changeClientVersion(true,context);
                    Utils.toast(context,"已将盒子切换成覆盖版支持");
                }else{
                    siwtch.setText("共存版");
                    Utils.changeClientVersion(false,context);
                    Utils.toast(context,"已将盒子切换成共存版支持");
                }
            }
        });

        linearLayout.addView(text1);
        linearLayout.addView(button);
        linearLayout.addView(button2);
        linearLayout.addView(siwtch);
        linearLayout.addView(button3);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File(Utils.getAppPath(context) + "/harmony/");
                if (file.exists()) {
                    Utils.toast(context, "文件夹已存在");
                } else {
                    Utils.toast(context,"已创建harmony文件夹，请将下载好的文件放入到harmony目录中");
                    file.mkdirs();
                }
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(httpData==null){
                    Toast.makeText(context,"网络请求失败，1秒钟后再试试",Toast.LENGTH_LONG).show();
                    return;
                }
                System.out.println("httpData:"+httpData);
                hud.show();
                final File file = new File(Utils.getAppPath(context) + "/harmony/");
                if(!file.exists()){
                    Utils.toast(context,"已创建harmony文件夹，请将下载好的文件放入到harmony目录中");
                    file.mkdirs();
                    return;
                }
                if (file.listFiles().length == 0) {
                    Utils.toast(context, "文件夹内为空，请放入harmony文件夹中");
                    return;
                }
                File xml = new File(file.getAbsolutePath() + "/Cocos2dxPrefsFile.xml");
                System.out.println(xml.getAbsoluteFile());
                if (xml.exists()) {
                    ClientUttls.install(context, xml, "../shared_prefs/", new ModInterface() {
                        @Override
                        public void onAction(boolean flag, int type) {
                            System.out.println("处理xml：" + flag);
                            if (flag) {
                                for (File listFile  : file.listFiles()) {
                                    System.out.println("处理文件：" + listFile.getAbsolutePath());
                                    if (listFile.getName().endsWith(".zip")) {
                                        File path;
                                        if (siwtch.isChecked()) {
                                            path = new File(Utils.getSDCardPath(context) + "/Android/data/com.huanmeng.zhanjian2/"+httpData);
                                            System.out.println("操作对象：覆盖版");
                                        } else {
                                            path = new File(Utils.getSDCardPath(context) + "/Android/data/com.bigzhao.jianrmagicbox/"+httpData);
                                            System.out.println("操作对象：共存版");
                                        }
                                        if (!path.exists()) {
                                            path.mkdirs();
                                        }
                                        System.out.println("准备处理zip");
                                        ZipUtils.unzip(listFile, path.getAbsolutePath(), hud);
                                        break;
                                    }
                                }
                                Utils.handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        hud.dismiss();
                                    }
                                });
                                Utils.toast(context, "操作完成");
                            }else{
                                Utils.toast(context, "Cocos2dxPrefsFile.xml处理失败");
                                hud.dismiss();
                            }
                        }
                    });
                } else {
                    Utils.toast(context, "Cocos2dxPrefsFile.xml文件不存在!");
                    hud.dismiss();
                }

            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://bbs.jianrmod.cn/forum.php?mod=viewthread&tid=186&extra="));
                context.startActivity(intent);
            }
        });
        new HttpTools().httpGet("http://jianr.jianrmod.cn/admin/data.do?data=harmony", new HttpInterface() {
            @Override
            public void httpGetData(Object data, int state) {
                httpData= (String) data;
            }

            @Override
            public void httpError(Exception e) {
                e.printStackTrace();
            }
        });
        return linearLayout;
    }

}
